文件下载程序下载PHP中的0字节文件
我的代码不是一直有效。大多数情况下,它下载0字节的图像。我可以通过此代码下载特定的图像,并且可能是此代码通过名称将图像的大小保存在现金中。如果我重命名图像,它将下载0字节文件下载程序下载PHP中的0字节文件,php,Php,我的代码不是一直有效。大多数情况下,它下载0字节的图像。我可以通过此代码下载特定的图像,并且可能是此代码通过名称将图像的大小保存在现金中。如果我重命名图像,它将下载0字节 $file_path= $full_path; $file = pathinfo($file_path); $base = $file['basename']; $dir = $file['dirname']; header('Content-Description: File Transfer'); header(
$file_path= $full_path;
$file = pathinfo($file_path);
$base = $file['basename'];
$dir = $file['dirname'];
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header("Content-Disposition: attachment; filename=".$base);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: no-cache');
header('Content-Length: ' . filesize($base));
ob_clean();
flush();
$path = $dir."/".$base;
readfile($path);
exit;
此代码修复了我的问题
function download($path)
{
// if file is not readable or not exists
if (!is_readable($path))
die('File does not exist or it is not readable!');
// get file's pathinfo
$pathinfo = pathinfo($path);
// set file name
$file_name = $pathinfo['basename'];
$mime = 'application/octet-stream';
// set headers
header('Pragma: public');
header('Expires: -1');
header('Cache-Control: public, must-revalidate, post-check=0, pre-check=0');
header('Content-Transfer-Encoding: binary');
header("Content-Disposition: attachment; filename=\"$file_name\"");
header('Content-Length: ' . filesize($path));
header("Content-Type: $mime");
// read file as chunk to reduce memory usages
if ( $fp = fopen($path, 'rb') ) {
ob_end_clean();
while( !feof($fp) and (connection_status()==0) ) {
print(fread($fp, 8192));
flush();
}
@fclose($fp);
exit;
}
}
download($file_path);
你检查过你的服务器的url\u fopen是否打开了吗?没有,我没有检查。服务器是我的电脑,它是在一个真正的IP。我怎么检查?