php下载功能给了我一个空文件

时间:2016-01-30 05:17:38

标签: php

继承我的代码

<?php

$file = $_GET['file'];

if (file_exists("uploaded_files\\" .$file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Length: ' . filesize($file));
    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($file));
    ob_clean();
    flush();
    readfile("uploaded_files\\" .$file);
    exit;
} else {
    echo "Download failed";
}
?>

我试图让用户下载他们在我的文件夹中上传的任何文件,名为uploaded_files

1 个答案:

答案 0 :(得分:0)

您需要更改文件位置,因为readfile()需要文件的绝对路径

<?php
$file = $_GET['file']
$file = "uploaded_files/".$file;

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));
    readfile($file);
    exit;
}else {
  echo "Download failed";
}
?>