下载文件没有下载预期的文件
Download file didn't download the expected file
我正在尝试下载已上传到 uploads
文件夹中的文件。目录是这样的:
xampp
> htdocs
> myProject
> normal_user
> displayfile.php <-- my current location
> uploads
> myfile.pdf
这是我的 HTML 代码中显示文件名的部分:
```
````
下面的下载代码可以执行,但它没有下载预期的文件。相反,它会下载一个 .PHP `file name: uploads` 和 `file type: All Files:
`
if (isset($_GET['forcedownload'])) {
$filepath = "M:/xampp/htdocs/myProject/uploads/" . $row_file['f_name']; // return: myfile.pdf
if (file_exists($filepath)) {
header('Content-Description: File Transfer');
header('Content-Type: ' . $row_file['f_type']); //return: application/pdf
header('Content-Disposition: attachment; filename="'.basename($filepath).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($filepath));
flush(); // Flush system output buffer
readfile($filepath);
exit();
} else {
exit();
}
}
`
我看过很多教程,也回答过 Stack Overflow 的问题,情况与我类似,但仍然发生同样的情况。我不知道我错过了什么...
echo $filepath;
查看这个变量的值。
它可能不包含您要下载的文件的名称。
我正在尝试下载已上传到 uploads
文件夹中的文件。目录是这样的:
xampp
> htdocs
> myProject
> normal_user
> displayfile.php <-- my current location
> uploads
> myfile.pdf
这是我的 HTML 代码中显示文件名的部分:
```
````
下面的下载代码可以执行,但它没有下载预期的文件。相反,它会下载一个 .PHP `file name: uploads` 和 `file type: All Files:
`
if (isset($_GET['forcedownload'])) {
$filepath = "M:/xampp/htdocs/myProject/uploads/" . $row_file['f_name']; // return: myfile.pdf
if (file_exists($filepath)) {
header('Content-Description: File Transfer');
header('Content-Type: ' . $row_file['f_type']); //return: application/pdf
header('Content-Disposition: attachment; filename="'.basename($filepath).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($filepath));
flush(); // Flush system output buffer
readfile($filepath);
exit();
} else {
exit();
}
}
`
我看过很多教程,也回答过 Stack Overflow 的问题,情况与我类似,但仍然发生同样的情况。我不知道我错过了什么...
echo $filepath;
查看这个变量的值。
它可能不包含您要下载的文件的名称。