【问题标题】:Download file didn't download the expected file下载文件未下载预期文件
【发布时间】:2021-06-15 07:38:26
【问题描述】:

我正在尝试下载我在uploads 文件夹中上传的文件。目录是这样的:

xampp
  > htdocs
     > myProject
        > normal_user
           > displayfile.php  <-- my current location
        > uploads
           > myfile.pdf

这是我的 HTML 代码中显示文件名的部分:

```
```` 下面的下载代码可以执行,但它没有下载预期的文件。相反,它会下载一个 .PHP `file name: uploads` 和 `file type: All Files:

`

    if (isset($_GET['forcedownload'])) {

      $filepath = "M:/xampp/htdocs/myProject/uploads/" . $row_file['f_name']; // return: myfile.pdf

      if (file_exists($filepath)) {
        header('Content-Description: File Transfer');
        header('Content-Type: ' . $row_file['f_type']); //return: application/pdf
        header('Content-Disposition: attachment; filename="'.basename($filepath).'"');
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize($filepath));
        flush(); // Flush system output buffer
        readfile($filepath);
        exit();
      } else {
        exit();
      }
      
    }

`

我已经看过许多教程,并回答了与我的情况相似的 Stack Overflow 问题,但它仍然发生相同的情况。我不知道我错过了什么......

【问题讨论】:

  • $filepath 的值是多少?
  • 对不起,如果这是一个明显的问题,但如何检查?
  • :-) 你可以这样写: echo $filepath;或 var_dump($filepath);就在 $filepath=... 行的下方
  • 哦,天哪,谢谢。值为M:/xampp/htdocs/myProject/uploads/
  • 一步一步,你现在知道如何做一个简单的调试了。不要忘记关闭主题。

标签: php force-download


【解决方案1】:
echo $filepath;

查看这个变量的值。

它可能不应该满足您要下载的文件的名称。

【讨论】:

    猜你喜欢
    • 2015-02-24
    • 1970-01-01
    • 2018-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多