如何使用php头从特定目录下载文件 [英] How to download a file from specific directory using php header
问题描述
我有一些使用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);
出口;
}
echo< h1>内容错误< / h1>< p>档案不存在!< / p>」;
}
?>
显示错误
内容错误
该文件不存在!
我正在使用
< a href =http://sap.layyah.info/download.php?link=UAC.dll =nofollow> http://sap.layyah.info/download.php?link=UAC.dll
此链接下载文件文件的原始位置是
http://sap.layyah.info/upload/UAC.dll
首先, $ file ='$ var_1';
不会被正确解释,
因此它需要读为 $ file = $ var_1;
您还有一个缺失的大括号}
<?php
if(isset($ _ GET ['link']))
{
$ var_1 = $ _GET ['link'];
$ file = $ var_1;
$ b $ 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);
出口;
}
} // - 丢失的大括号
?>
你提到你想使用不同的文件夹。
您可以使用以下内容:
$ dir =folder /; //后面的斜杠很重要
$ file = $ dir。 $ VAR_1;
或
$ dir =../folder/; //后面的斜杠很重要
$ file = $ dir。 $ VAR_1;
取决于文件夹的位置。
编辑
以下内容经过测试并适用于我,并且这些文件是从我的服务器的根目录运行的。
<?php
if(isset($ _ GET ['link']))
{
$ var_1 = $ _GET ['link'];
// $ file = $ var_1;
$ dir =folder /; //后面的斜杠很重要
$ file = $ dir。 $ VAR_1;
$ b $ 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);
出口;
}
} // - 丢失的大括号
?>
HTML (我以PDF文件为例)
< a href =download.php?link = document.pdf>在此下载< / a>
I have some code to download file using php header but it is not properly working and want to add directory to read
<?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
First, the quotes in $file = '$var_1';
won't get interpreted correctly,
therefore it needs to read as $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)
<a href="download.php?link=document.pdf">Download here</a>
这篇关于如何使用php头从特定目录下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!