php 如何使用php标头从特定目录下载文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22265472/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to download a file from specific directory using php header
提问by user2740125
I have some code to download file using php header but it is not properly working and want to add directory to read
我有一些代码可以使用 php 标头下载文件,但它无法正常工作并想添加目录以进行读取
<?php
if(isset($_GET['link'])){
$var_1 = $_GET['link'];
$dir='/upload/';
}
?>
<?php
if(isset($_GET['link'])){
$var_1 = $_GET['link'];
$file = $var_1;
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
echo "<h1>Content error</h1><p>The file does not exist!</p>";
}
?>
It shows error Content error
它显示错误内容错误
The file does not exist!
该文件不存在!
I am Using
我在用
this link to download the file file original location is
这个下载文件文件原始位置的链接是
回答by Funk Forty Niner
First, the quotes in $file = '$var_1';won't get interpreted correctly,
首先,引号$file = '$var_1';不会被正确解释,
therefore it needs to read as $file = $var_1;
因此它需要读作 $file = $var_1;
You also have a missing closing brace }
您也缺少右括号 }
<?php
if(isset($_GET['link']))
{
$var_1 = $_GET['link'];
$file = $var_1;
if (file_exists($file))
{
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
} //- the missing closing brace
?>
And you mentioned that you wanted to use a different folder.
你提到你想使用不同的文件夹。
You could use something to the effect of:
你可以使用一些东西来达到以下效果:
$dir = "folder/"; // trailing slash is important
$file = $dir . $var_1;
or
或者
$dir = "../folder/"; // trailing slash is important
$file = $dir . $var_1;
depending on the folder's location.
取决于文件夹的位置。
Edit
编辑
The following is tested and worked for me and the files were run from the root of my server.
以下内容经过测试并为我工作,文件是从我的服务器的根目录运行的。
<?php
if(isset($_GET['link']))
{
$var_1 = $_GET['link'];
// $file = $var_1;
$dir = "folder/"; // trailing slash is important
$file = $dir . $var_1;
if (file_exists($file))
{
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
} //- the missing closing brace
?>
HTML(I used a PDF file as an example)
HTML(我以 PDF 文件为例)
<a href="download.php?link=document.pdf">Download here</a>

