a question about directory browsing via php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

a question about directory browsing via php

Post 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
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post 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, ;)
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post 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
Last edited by scorphus on Thu Jun 03, 2004 3:31 pm, edited 1 time in total.
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post 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
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Ya, look at [php_man]is_dir[/php_man].
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post 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.
Post Reply