PHP:Force download正在下载一个空文件
下面的代码显示:PHP:Force download正在下载一个空文件,php,download,Php,Download,下面的代码显示: /home/my_site/www/wp-content/uploads/2017/03/2017-03-17-my_file.mp3 据我所知,这条路是正确的。然而,当我注释掉echo并下载该文件时,它是一个空文件。为什么? 代码: if (isset($_GET['file'])) { clearstatcache(); $file_path = str_replace('http://www.example.com/', '', $_GET['file
/home/my_site/www/wp-content/uploads/2017/03/2017-03-17-my_file.mp3
据我所知,这条路是正确的。然而,当我注释掉echo
并下载该文件时,它是一个空文件。为什么?
代码:
if (isset($_GET['file'])) {
clearstatcache();
$file_path = str_replace('http://www.example.com/', '', $_GET['file']);
$file_path = $_SERVER['DOCUMENT_ROOT'] . '/' . $file_path . '.mp3';
echo $file_path;
if(file_exists($file_path)) {
$file_name = basename($file_path);
$file_size = filesize($file_path);
header("Cache-Control: private");
header("Content-Type: application/stream");
header("Content-Length: ".$file_size);
header("Content-Disposition: attachment; filename=".$file_name);
exit();
}
else {
die('The provided file path is not valid.');
}
}
编辑:这是我在KIKO软件公司的建议后得到的。仍然下载空文件
<?php
if (isset($_GET['file'])) {
$file_path = $_SERVER['DOCUMENT_ROOT'] . '/' . $_GET['file'] . '.mp3';
//echo $file_path;
//$file_path = $_SERVER['SCRIPT_FILENAME'];
if (file_exists($file_path)) {
$file_name = basename($file_path);
$file_size = filesize($file_path);
header('Content-type: application/octet-stream');
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.$file_size);
header('Content-Disposition: attachment; filename='.$file_name);
//readfile($file_path);
exit();
}
else {
die('The provided file path is not valid.');
}
}
?>
下载文件的简单示例:
$content = 'This is the content.';
$size = strlen($content);
$name = 'test.txt';
header('Content-type: application/octet-stream');
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.$size);
header('Content-Disposition: attachment; filename='.$name);
echo $content;
首先要确保这是有效的。如果没有,则标题可能有问题。打开错误报告并查看:
那就在这基础上继续努力吧。首先下载脚本本身:
$file_path = $_SERVER['SCRIPT_FILENAME'];
if (file_exists($file_path)) {
$file_name = basename($file_path);
$file_size = filesize($file_path);
header('Content-type: application/octet-stream');
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.$file_size);
header('Content-Disposition: attachment; filename='.$file_name);
readfile($file_path);
exit();
}
else {
die('The provided file path is not valid.');
}
只有在那之后,才能尝试下载其他东西
通过一步一步地处理问题,可以更容易地看到问题出在哪里。下载文件的简单示例:
$content = 'This is the content.';
$size = strlen($content);
$name = 'test.txt';
header('Content-type: application/octet-stream');
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.$size);
header('Content-Disposition: attachment; filename='.$name);
echo $content;
首先要确保这是有效的。如果没有,则标题可能有问题。打开错误报告并查看:
那就在这基础上继续努力吧。首先下载脚本本身:
$file_path = $_SERVER['SCRIPT_FILENAME'];
if (file_exists($file_path)) {
$file_name = basename($file_path);
$file_size = filesize($file_path);
header('Content-type: application/octet-stream');
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.$file_size);
header('Content-Disposition: attachment; filename='.$file_name);
readfile($file_path);
exit();
}
else {
die('The provided file path is not valid.');
}
只有在那之后,才能尝试下载其他东西
通过一步一步地解决问题,可以更容易地看出问题出在哪里。如果我这样做,Firefox崩溃,根本没有文件下载。。。在Chrome中,我看到了文件的(胡言乱语)内容,也没有文件下载。我必须承认,我没有测试你的代码。
readfile()
是正确的,因此这不会导致出现问题。我稍微改变了你的标题,也许会有帮助?它们不是标准的。使用url时,请阅读readfile()
上的手册。如果已启用fopen包装器,则URL可以用作此功能的文件名。另请参见:如果您希望他人提供工作代码。谢谢!但是,您的代码也会触发空文件的下载。请尝试简单内容。因此,与其读取和输出文件,不如简单地echo'1234567890'
并查看是否可以下载。在那之后,确保你的PHP有读取文件的权限。如果我这样做了,Firefox崩溃,根本没有文件下载。。。在Chrome中,我看到了文件的(胡言乱语)内容,也没有文件下载。我必须承认,我没有测试你的代码。readfile()
是正确的,因此这不会导致出现问题。我稍微改变了你的标题,也许会有帮助?它们不是标准的。使用url时,请阅读readfile()
上的手册。如果已启用fopen包装器,则URL可以用作此功能的文件名。另请参见:如果您希望他人提供工作代码。谢谢!但是,您的代码也会触发空文件的下载。请尝试简单内容。因此,与其读取和输出文件,不如简单地echo'1234567890'
并查看是否可以下载。之后,确保您的PHP具有读取该文件的权限。