Page 1 of 1

a question about directory browsing via php

Posted: Thu Jun 03, 2004 3:11 pm
by dull1554
ok so heres the deal, ive grown to be a bit of a security freak.....
i like there to be a index file in every folder on my webserver, now it would not be a problem if i was the only person who has ftp access but im not, and many people create new dirs, so i want to write a cron that will, say once a week run through the entire directory structure checking for a index.php file in every folder, if there is one to pass on to the next folder, and if there is not one to create a blank index.php file

i have used this code to create an array of all the files and folders

Code: Select all

<?php

$folder = "/";
if($handle = opendir($folder))
{
    for($i = 0; $files = readdir($handle); $i++)
    {
        if ($files != "." && $files != ".." && $files != "Thumbs.db")
        {
            $array[$i] = $files;
        }

    }
}
?>
could someone guide me in the correct direction, not sure really where to go from here, or if it is really even possiable
thanx

Posted: Thu Jun 03, 2004 3:27 pm
by Joe
I think the best option is to use the file_exists() function then the fopen() function with an +a mode. This will write the file if it doesn't exist im sure. Heres a quick sketch...

<?php
$file = '/filepath/';

if (file_exists($file))
{
echo "$file exists";
}
else
{
fopen($file, "+a");
}
?>


It will not do the whole server, thats up to you to figure out, ;)

Posted: Thu Jun 03, 2004 3:29 pm
by scorphus
You might want to use a recurring function. Something like:

Code: Select all

function checkDir (dir) &#123;
  opend dir
  if necessary
    create index.php
  check each dir entry
    if entry is a directory
      checkDir(entry)
  return
&#125;
-- Scorphus

Posted: Thu Jun 03, 2004 3:31 pm
by dull1554
ok then, heres another question, is there a way to use read_dir() to return a list of just directorys, that way i could build an array of the directorys like

Code: Select all

array(
&#1111;0] => admin
&#1111;1] => admin/blah
.....
......

....
...
..
and so on so i could just loop through and use file_exists() and see if it is in each dir

Posted: Thu Jun 03, 2004 4:58 pm
by pickle
Ya, look at [php_man]is_dir[/php_man].

Posted: Fri Jun 04, 2004 7:45 am
by launchcode
Silly question - but why not just disable directory browsing on your server? Then it doesn't matter if you've an index page or not, no-one can view the contents list anyway.