dongzhou4727 2016-11-04 18:43
浏览 124
已采纳

目录下载链接无法正常工作“服务器错误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 2016-11-07 20:36
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了