Readfile returns 一个空文件
Readfile returns an empty file
我有强制下载的代码,$file
是现有 .jpg、.png 或 .pdf 文件的 url(我确保它存在)
<?php
$file = $_REQUEST['file'];
$file_extension = end(explode('.', $file));
$file_name = end(explode('/', $file));
switch ($file_extension) {
case 'jpg':
header('Content-Type: image/jpeg');
header('Content-Disposition: attachment; filename='.$file_name);
header('Pragma: no-cache');
readfile($file);
break;
case 'png':
header('Content-Type: image/png');
header('Content-Disposition: attachment; filename='.$file_name);
header('Pragma: no-cache');
readfile($file);
break;
case 'pdf':
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename='.$file_name);
header('Pragma: no-cache');
readfile($file);
break;
}
但它正在下载一个空 (0KB) 文件(名称正确)
想过会发生什么吗?
那是因为你少了一个 Content-Length header.
试试这个:
$file = $_REQUEST['file'];
$file_extension = end(explode('.', $file));
$file_name = end(explode('/', $file));
switch ($file_extension) {
case 'jpg':
header('Content-Type: image/jpeg');
break;
case 'png':
header('Content-Type: image/png');
header('Content-Disposition: attachment; filename='.$file_name);
break;
case 'pdf':
header('Content-Type: application/pdf');
break;
}
header('Content-Disposition: attachment; filename='.$file_name);
header('Pragma: no-cache');
header('Content-Length: ' . filesize($file));
// You may want to add this headers too (If you don't want the download to be resumable - I think).
header('Expires: 0');
header('Cache-Control: must-revalidate');
// And you may consider flushing the system's output buffer if implicit_flush is turned on in php.ini.
flush();
// If you have the file locally.
readfile($file);
// Otherwise,
echo file_get_contents($file); // You should have allow_url_include on in php.ini
还没来得及尝试,但应该可以。
由于 file_get_contents() return 也为 null,您的问题可能是 php.ini 配置中的设置。
参数allow_url_fopen必须On.
我有强制下载的代码,$file
是现有 .jpg、.png 或 .pdf 文件的 url(我确保它存在)
<?php
$file = $_REQUEST['file'];
$file_extension = end(explode('.', $file));
$file_name = end(explode('/', $file));
switch ($file_extension) {
case 'jpg':
header('Content-Type: image/jpeg');
header('Content-Disposition: attachment; filename='.$file_name);
header('Pragma: no-cache');
readfile($file);
break;
case 'png':
header('Content-Type: image/png');
header('Content-Disposition: attachment; filename='.$file_name);
header('Pragma: no-cache');
readfile($file);
break;
case 'pdf':
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename='.$file_name);
header('Pragma: no-cache');
readfile($file);
break;
}
但它正在下载一个空 (0KB) 文件(名称正确)
想过会发生什么吗?
那是因为你少了一个 Content-Length header.
试试这个:
$file = $_REQUEST['file'];
$file_extension = end(explode('.', $file));
$file_name = end(explode('/', $file));
switch ($file_extension) {
case 'jpg':
header('Content-Type: image/jpeg');
break;
case 'png':
header('Content-Type: image/png');
header('Content-Disposition: attachment; filename='.$file_name);
break;
case 'pdf':
header('Content-Type: application/pdf');
break;
}
header('Content-Disposition: attachment; filename='.$file_name);
header('Pragma: no-cache');
header('Content-Length: ' . filesize($file));
// You may want to add this headers too (If you don't want the download to be resumable - I think).
header('Expires: 0');
header('Cache-Control: must-revalidate');
// And you may consider flushing the system's output buffer if implicit_flush is turned on in php.ini.
flush();
// If you have the file locally.
readfile($file);
// Otherwise,
echo file_get_contents($file); // You should have allow_url_include on in php.ini
还没来得及尝试,但应该可以。
由于 file_get_contents() return 也为 null,您的问题可能是 php.ini 配置中的设置。
参数allow_url_fopen必须On.