无法使用PHP从我的文件夹下载我的文件

时间:2016-01-19 16:56:33

标签: php html

我目前正在使用localhost运行我的页面,目前我正在尝试下载我的用户上传并存储在名为uploaded_files的文件夹中的文件

这是我的下载页面的代码无效,我不太确定是什么问题。

<?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;
}
?>

3 个答案:

答案 0 :(得分:0)

file_exists()需要接收文件的路径,如果文件名为"test"且位于"uploaded_files/",则需要检查file_exists("uploaded_files/test")。如果您的php已经在"FYPproject"文件夹中,则无需传递整个路径。下载文件时也是如此,您无需传递readfile()的整个路径。

答案 1 :(得分:0)

您正在将带有反斜杠和正斜杠的文件路径传递给readfile。即使这不是您的问题的原因,您应该更改它。将其更改为:

readfile("C:\xampp\htdocs\FYPproject\uploaded_files\".$file);

答案 2 :(得分:0)

并非所有浏览器都支持application/force-download,因此在这种情况下,请尝试将上述代码替换为application/octet-stream并查看其是否有效

相关问题