Evan,

Here is a super simple function I wrote to help things out with
directories.

function dir_list($input) {
        $dir = @opendir($input) or die ("Invalid Directory");
        $dirlist = array();
        while ($dirlist = readdir($dir)) {
            $file[] = $dirlist;
        }
        closedir($dir);
        sort($file);
        return $file;
}

You would use it like this:

<?php
include("include.inc");
if(isset($_GET['dir'])) {
    $array = dir_list($_GET['dir']);
    //processing here
    foreach ($array as $file) {
        if (is_dir($file)) {
            echo "$file is a directory<br>\n";
        }
        else {
            echo "$file is a file<br>\n";
        }
    }
}
?>

Obviously, you could do whatever you wanted after getting the list in the
array. You should be aware of security risks with this approach, though.
If the user inputs ../../winnt/ or something, they would be able to see
what is in those directories. The simple way around this (that solves some
of the problem) is to turn on open_basedir (see php.net for how to set
this). Then the directory reading will be restricted to whatever is under
those directories.

You may want to look into the getcwd(), dirname(), basename() and
realpath() functions.

Good luck.

-Dash

"I don't believe in astrology.  But then I'm an Aquarius, and Aquarians
don't believe in astrology."
                -- James R. F. Quirk

On Sun, 9 Feb 2003, Evan wrote:

> Working with PHP 4.2.3==> IIS5.1, WinXP Pro, NTFS
>
> I made this simple page to be able to brouse throught folders and files, but
> it seems that it doesn't recognize folders when I go outside the directory
> in which is contained the .php file.
> Any Ideas?
> Thanks
>
> *************************+++
> <?php
> $nome_dir = "";
> if( isset($_GET['folder']))
>  $nome_dir = $_GET['folder']."/";
> if ($handle = opendir($nome_dir)) {
>     echo "Directory handle: $handle<br>";
>     $i=0;
>     $j=0;
>     while (false !== ($file = readdir($handle))) {
>      if(is_dir($file)){
>          echo "<b>$file</b><br>";
>          $directory[$j]=$file;
>          $j++;
>         }else{
>          echo "$file<br>";
>   $elenco_file[$i]=$file;
>   $i++;
>         }
>     }
>  //Ordinamento per nome delle directory e dei file:
>  if(is_array($directory))
>   sort($directory);
>  if(is_array($elenco_file))
>   sort($elenco_file);
>
>  //Stampa i valori
>  //print_r($directory);
>  //print_r($elenco_file);
>
>
>  echo "<br><br><br>";
>
>  for($i=0;$i<count($directory);$i++){
>   echo "<b><a href =
> \"dir_list.php?folder=".$nome_dir.$directory[$i]."\">".$directory[$i]."</a><
> /b><br>";
>  }
>  for($i=0;$i<count($elenco_file);$i++){
>   echo $elenco_file[$i]."<br>";
>  }
>
>     closedir($handle);
> }
> ?>
>
>
>
> --
> PHP Windows Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


-- 
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to