[英]Unable to download my files from my folder using PHP
I'm currently using localhost to run my pages and currently I am trying to download the files my users have uploaded and stored in a folder called uploaded_files 我当前正在使用localhost来运行我的页面,并且当前正在尝试下载用户已上传并存储在名为Uploaded_files的文件夹中的文件
this is the code for my download page which isn't working and I'm not quite sure what's wrong. 这是我的下载页面的代码,该代码无法正常工作,我不太确定出了什么问题。
<?php
$file = $_GET['file'];
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/force-download');
header("Content-Disposition: attachment; filename=\"" . basename($file) . "\";");
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($name));
ob_clean();
flush();
readfile("C:\xampp\htdocs\FYPproject\uploaded_files/".$file); //showing the path to the server where the file is to be download
exit;
} else {
echo "Download failed";
echo $file;
}
?>
file_exists()
need to receive the path of the file, if the file name is "test"
and it's in "uploaded_files/"
so you need to check file_exists("uploaded_files/test")
. 如果文件名是"test"
并且在"uploaded_files/"
,则file_exists()
需要接收文件的路径,因此您需要检查file_exists("uploaded_files/test")
。 You don't need to pass the whole path if your php is already in "FYPproject"
folder. 如果您的php已经在"FYPproject"
文件夹中,则无需传递整个路径。 It's the same for downloading files, you don't need to pass the whole path for readfile()
. 下载文件是相同的,您不需要传递readfile()
的整个路径。
You are passing a file path with backward slashes and forward slashes to readfile
. 您正在将带有反斜杠和正斜杠的文件路径传递给readfile
。 Even if this is not the cause of your problem you should change it. 即使这不是造成问题的原因,也应更改它。 Change it to this: 更改为此:
readfile("C:\\xampp\\htdocs\\FYPproject\\uploaded_files\\".$file);
并非所有浏览器都支持application/force-download
因此在这种情况下,请尝试用application/octet-stream
替换上面的代码,看看是否可行
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.