dongzhou4727
dongzhou4727
2016-11-04 18:43

目录下载链接无法正常工作“服务器错误404 - 找不到文件或目录。”

已采纳

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.

<?php
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"));

            }
        }

    $d->close();

return $retval;

}
?>

<?php
    $dirlist = getFileList("docs");
    print "<table id='myTable' border=\"1\">
";
    print "<thead>
";
    print "<tr class='header'><th>File Name</th><th>Link (click to download)</th><th>Size</th><th>Last Modified</th></tr>
";
    print "</thead>
";
    print "<tbody>
";
        foreach($dirlist as $file) {
            print "<tr>
";
            print "<td>{$file['title']}</td>
";
            $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>
";
            print "<td>{$file['size']}</td>
";
            $timestamp = date('F d Y h:i A', $file['lastmod']);
            print "<td>{$timestamp}</td>
";
            print "</tr>
";
        }   
  print "</tbody>";
  print "</table>";
 ?>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • dreamy6301 dreamy6301 5年前

    I fixed it.

    Change this line:

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

    To this:

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

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

    点赞 评论 复制链接分享
  • dongyou2714 dongyou2714 5年前

    Try changing

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

    Into

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

    You didn't quote the actual href part of the link.

    点赞 评论 复制链接分享

为你推荐