리디렉션없이 파일을 다운로드하거나 다른 파일을 사용하여 큰 zip 파일을 다운로드하려는 링크를 얻으려고합니다.큰 파일에서 PHP 다운로드가 실패합니다.
이 파일은 ~ 100MB 미만의 파일에 적합하지만 그 이상인 파일은 Error code: ERR_INVALID_RESPONSE
이라는 크롬의 오류 페이지로 전송됩니다.
$ file 및 $ path는 jQuery와 함께 양식에 넣어지고 $('form').submit();
이 호출됩니다.
function downloadFile($file, $path){
set_time_limit(0);
$fullPath = $path . $file;
if ($fd = fopen ($fullPath, "r")) {
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
$ext = strtolower($path_parts["extension"]);
switch ($ext) {
case "pdf":
header("Content-type: application/pdf"); // add here more headers for diff. extensions
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a download
break;
default;
header("Content-type: application/octet-stream");
header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
}
header("Content-length: $fsize");
header("Cache-control: private"); //use this to open files directly
while(!feof($fd)) {
$buffer = fread($fd, 2048);
echo $buffer;
}
}
fclose ($fd);
exit;
}
대용량 파일에서 실패한 이유는 무엇입니까?
업데이트 : 나는 다음과 같은 코드를 사용하면 , 그것은 파일을 다운로드하지만, 어떤 이유로 손상됩니다으로 완성 된 파일이 열리지 않습니다 :
set_time_limit(0);
ini_set('memory_limit', '1024M');
// http headers for downloads
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".$filename."\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".$file_size);
ob_end_flush();
@readfile($fileinfo['path']);
이 코드를 사용, 그것은 단, 첫 번째 49.7kb 다음이 완료 말한다 다운로드 난 함께 ob_flush()
및 flush()
을 시도 :
// http headers for downloads
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".$filename."\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($fileinfo['path']));
if ($fd = fopen ($fileinfo['path'], "r")) {
set_time_limit(0);
ini_set('memory_limit', '1024M');
while(!feof($fd)) {
echo fread($fd, 4096);
flush();
}
}
최대 업로드 크기 제한이 php.ini 파일에 있는지 확인하십시오. –
그게 문제인지 잘 모르겠 으면 내 업데이트를 살펴보십시오. – David