Jacob Mcdowell Jacob Mcdowell - 9 months ago 35
PHP Question

Directory Download Link not working "Server Error 404 - File or directory not found."

I am working on an Intranet with a searchable document repository page. It uses some php to generate a list of documents in a directory and put them in a table. When I click the link in the second column it should download the document but instead shows an error page with the phrase "404 - File or directory not found. The resource you are looking for might have been removed, had its name changed, or is temporarily unavailable."

I'm using Windows server 2012 r2 and ISS 7.5.

I double checked and the files are definitely in the correct directory.

Here's the code for the webpage.

function getFileList($dir)
$retVal = array();
if (substr($dir, -1) != "/") $dir .= "/";

// open pointer to directory and read list of files
$d = @dir($dir) or die("getFileList: Failed opening directory $dir for reading");

while(false !== ($entry = $d->read())) {
// skip hidden files
if($entry[0] == ".") continue;

if(is_dir("$dir$entry")) {
$filename = "$dir$entry";
$retval[] = array(
"title" => "$filename/",
"path" => filetype("$filename"),
"size" => 0,
"lastmod" => filemtime("$filename"));
} elseif(is_readable("$dir$entry")) {
$filename = "$dir$entry";
$retval[] = array(
"title" => "$entry",
"path" => "$filename",
"size" => filesize("$filename"),
"lastmod" => filemtime("$filename"));



return $retval;


$dirlist = getFileList("docs");
print "<table id='myTable' border=\"1\">\n";
print "<thead>\n";
print "<tr class='header'><th>File Name</th><th>Link (click to download)</th><th>Size</th><th>Last Modified</th></tr>\n";
print "</thead>\n";
print "<tbody>\n";
foreach($dirlist as $file) {
print "<tr>\n";
print "<td>{$file['title']}</td>\n";
$path_parts = pathinfo($file['path']);
$path_noext = $path_parts['dirname'] . "/" . $path_parts['filename'];
$path_noext = str_replace(' ', '%20', $path_noext);
print "<td><a href=pdf_download.php?filename={$path_noext}>{$file['title']}</a></td>\n";
print "<td>{$file['size']}</td>\n";
$timestamp = date('F d Y h:i A', $file['lastmod']);
print "<td>{$timestamp}</td>\n";
print "</tr>\n";
print "</tbody>";
print "</table>";

Answer Source

I fixed it.

Change this line:

print "<td><a href=pdf_download.php?filename={$path_noext}>{$file['title']}</a></td>\n";

To this:

print "<td><a href=docs/{$file['title']}>{$file['title']}</a></td>\n";

It couldn't find the files because the download link was pointed to the root directory not to the documents directory.