Link to home
Start Free TrialLog in
Avatar of BrighteyesDesign
BrighteyesDesignFlag for Afghanistan

asked on

View Directory on website using php

I have setup a function for my client to upload files via a php form to a specific directory.

What i need to do is display the directory files on a webpage so my client can see whats been uploaded. Also, if this is possible is it then possible to delete files? (not so important but would be useful)

Thanks
ASKER CERTIFIED SOLUTION
Avatar of ludofulop
ludofulop

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ludofulop
ludofulop

for deleting files, use unlink() of course:

http://sk.php.net/manual/en/function.unlink.php
hi,

Use this,



<?php
$link = "documents";
if(is_link($link)) {
      $dir = readlink($link);

      if($dh = opendir($dir)) {
            while(($file = readdir($dh)) !== false) {
                  echo "filename: $file -- filetype ".filetype($dir.$file)."\n";
            }
            closedir($dh);
      }
}
?>

Open in new window

Avatar of Loganathan Natarajan
try this,

ref. http://php.net/manual/en/function.readdir.php

<?php
// Note that !== did not exist until 4.0.0-RC2

if ($handle = opendir('/path/to/files')) {
    echo "Directory handle: $handle\n";
    echo "Files:\n";

    /* This is the correct way to loop over the directory. */
    while (false !== ($file = readdir($handle))) {
        echo "$file\n";
    }

    /* This is the WRONG way to loop over the directory. */
    while ($file = readdir($handle)) {
        echo "$file\n";
    }

    closedir($handle);
}
?>

Open in new window

Avatar of BrighteyesDesign

ASKER

Thanks all!

ludofulop, I have used that script and the directory contents show fine. They show as a list though, what would i add to the code the have each filename appear on a new line?

thanks
just replace \n with br in echo command:

echo "filename: $file -- filetype ".filetype($dir.$file)."<br />"