Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PHP Script for add index.html into every site folder(Recursively).
<?php
/**
* Protect you site folder from directory listings
* PHP Script for add index.html into every site folder(Recursively).
* @author Manish Trivedi
* @Email manish.tech2@gmail.com
*
* HOW TO USE :
* 1#. Put into your root directory (where you want to start added index.html recursively)
* 2#. Open this file in url and It will be executed ( ROOT_PATH/create_index_html.php)
* 3#. You can see all dir where index.html added as a output. (also added add_index.log in your site root folder)
*
* @NOTE: At that moment, I did not test this script with various site permissions.
*/
define('DS', DIRECTORY_SEPARATOR);
$currentDir = dirname(__FILE__); // site folder
AddIndexFile::scan($currentDir);
AddIndexFile::log($currentDir);
echo "\n ####################### Done! ###########################" ;
class AddIndexFile
{
static $addedIndexDir = array();
static function scan($currentDir)
{
// Scan current dir
$dirList = scandir($currentDir);
//Check index.html exit or not if not then add it
$files = Array('index.htm','index.html','index.php'); // predefine files
// if any one file exist then it will be available into $result array
$result = array_intersect($files, $dirList);
if(empty($result)) {
self::create($currentDir);
}
foreach ($dirList as $file) {
if ($file === '.' or $file === '..') {
continue;
}
if( is_dir($currentDir.DS.$file)) {
self::scan($currentDir.DS.$file);
}
}
}
static public function create($dir)
{
echo "$dir <br/>";
self::$addedIndexDir[] = $dir;
// content of inde.html
$string = "<html><body bgcolor='#FFFFFF'></body></html>";
//open file in write mode
$fp = fopen($dir.DS.'index.html', 'w');
// file will acquire an exclusive lock (writer).
flock($fp, LOCK_EX);
//write info file
fwrite($fp, $string);
// release a lock from file
flock($fp, LOCK_UN);
}
static public function log($dir)
{
$string = '###################### index.html Added following directory ######################'."\n\n";
$string .= implode("\n", self::$addedIndexDir);
//open file in write mode
$fp = fopen($dir.DS.'add_index.log', 'a');
// file will acquire an exclusive lock (writer).
flock($fp, LOCK_EX);
//write info file
fwrite($fp, $string);
// release a lock from file
flock($fp, LOCK_UN);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.