本文介绍了如何使用php头从特定目录下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有一些使用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>」; } ?> 显示错误内容错误 该文件不存在! 我正在使用 此链接下载文件文件的原始位置是 解决方案首先, $ 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 <?phpif(isset($_GET['link'])){ $var_1 = $_GET['link'];$dir='/upload/';}?><?phpif(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 errorContent errorThe 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 }<?phpif(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.EditThe following is tested and worked for me and the files were run from the root of my server.<?phpif(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头从特定目录下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-24 06:40