I am having some trouble making a file/directory manager in PHP.
This is the code ...
<?php
// set directory name and open it
$search_dir="./";
$dp=opendir($search_dir);
// list directories
echo "<h2>Directories</h2>\n";
echo "<ul>";
while($item=readdir($dp))
{
if(is_dir($item))
{
echo "<li>".$item."</li>\n";
}
}
echo "</ul>\n";
// reset pointer
rewinddir($dp);
// list files
echo "<h2>Files</h2>\n";
echo "<table cellpadding=\"2\" cellspacing=\"0\">\n";
echo "<tr><td><b>Name</b></td><td><b>Size</td><td><b>Last modified</b></td></tr>\n";
while($item=readdir($dp))
{
if(is_file($item))
{
// get file size
$fs=filesize($item);
// get last modified
$lm=date("Y-m-d", filemtime($item));
// print information
echo "<tr><td>".$item."</td><td>".$fs." bytes</td><td>".$lm."</td></tr>\n";
}
}
echo "</table>";
// close directory
closedir($dp);
?>
The above code produces the following output.
Directories
* .
* ..
* images
* downloads
Files
Name Size Last modified
contact.php 3571 bytes 2004-05-14
count.php 566 bytes 2004-05-14
count.txt 3 bytes 2004-05-15
downloads.txt 307 bytes 2004-05-14
files.php 4635 bytes 2004-05-14
footer.php 1120 bytes 2004-05-14
forbidden.php 301 bytes 2004-05-14
global.css 3702 bytes 2004-05-15
global.js 293 bytes 2004-05-14
header.php 1831 bytes 2004-05-14
home.php 1053 bytes 2004-05-14
index.php 532 bytes 2004-05-14
links.php 4388 bytes 2004-05-14
notfound.php 304 bytes 2004-05-14
unavailable.php 311 bytes 2004-05-14
... etc
Now all that works fine, but my problem is when I change the $search_dir variable to a different directory, it won't list the directory's contents.
In other words, I want to change ...
$search_dir="./";
... to ...
$search_dir="./images";
... so it lists the contents of the "images" directory, but when I do, it doesn't list anything even though that directory contains files and directories and has read permissions.