从绝对路径下载文件

4

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

$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); - Viktor
抱歉,问题中有一个打字错误。我正在使用相同的字符串。 - user2806026
我想我已经解决了,但是由于某种原因,返回的文件名包含了整个文件路径。我能避免这种情况吗?例如:-home-mydomain-public_html-wp-content-uploads-safefiles-312tekstsecure3.pdf - user2806026
当然。如果您想直接从文件中读取内容,请使用以下方式的finfo_file()$finfo = finfo_open(FILEINFO_MIME_TYPE); $mime = finfo_file($finfo, $fileurl); finfo_close($finfo); header("Content-type:" . $mime);,而不是仅使用header("Content-type:application/pdf"); - Viktor
当输出 $mime 时,它似乎为空。 - user2806026
显示剩余3条评论
3个回答

9

您只需要更改一行代码。

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

请给我的回答点赞,这样它就可以对其他人有帮助了,谢谢 :)。 - Kishan
2
问题和答案之间有什么区别? - Sam

0

应该是

readfile($fileurl)

0

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接