【问题标题】:Download file from absolute path从绝对路径下载文件
【发布时间】:2016-08-03 12:40:24
【问题描述】:

我正在尝试从绝对路径下载文件,但无法正常工作。我做错了什么?

$fileurl = '/home/mydomain/public_html/wp-content/uploads/312tekstsecure3.pdf';
header("Content-type:application/pdf");
header('Content-Disposition: attachment; filename=' . $fileurl);
readfile( $fileurl );

【问题讨论】:

  • 因为你的变量名是$fileurl,你也应该有readfile( $fileurl );
  • 对不起。在问题中犯了一个错字。我使用的是相同的字符串。
  • 我想我让它工作了,但出于同样的原因,返回的文件将整个文件路径作为名称。我可以避免这种情况吗?即:-home-mydomain-public_html-wp-content-uploads-safefiles-312tekstsecure3.pdf
  • 是的,你可以使用header('Content-Disposition: attachment; filename=' . basename($fileurl));来设置文件名而不是整个路径。
  • 太棒了!这很完美。我还可以更改内容类型以允许下载任何文件类型吗?

标签: php header readfile absolute-path


【解决方案1】:

您只需要更改一行。

<?php 
$fileurl = 'yourpath/file.pdf';
header("Content-type:application/pdf");
header('Content-Disposition: attachment; filename=' . $fileurl);
readfile( $fileurl );
?>

【讨论】:

  • 请对我的回答进行投票,以便对其他人有所帮助,谢谢:)。
  • 问题和答案有什么区别?
【解决方案2】:

应该是

readfile($fileurl)

【讨论】:

    【解决方案3】:

    您确定该文件存在并且服务器对该文件具有正确的访问权限吗? Content-Length 标头呢?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-17
      • 2011-07-09
      • 1970-01-01
      • 2021-10-18
      • 2014-03-02
      • 2012-10-15
      相关资源
      最近更新 更多