http — скачать с PHP — & gt; ERR_CONTENT_LENGTH_MISMATCH

Я хотел бы загрузить PDF с PHP с моего сервера, который также загружается с помощью PHP. Хотя это работает с некоторыми файлами, я получаю следующее сообщение об ошибке с отдельными файлами:

ERR_CONTENT_LENGTH_MISMATCH

Это моя функция загрузки:

if($_GET['funktion'] == 'downloadverwaltung')
{$filename = basename($dateiname);
$path = '/kunden/261105_71522/webseiten/admin/PDF/fern_downloads/'.$rowkurs['courseid'].'/'.$filename.''; // the file made available for download via this PHP file

if($_SESSION['xle'][$_GET['idsh']]['letype'] == 'fern' && $_GET['link'] == 'ja')
{
$path = 'http://ls.gdvz.de/'.$filename.'';
}
elseif($_SESSION['xle'][$_GET['idsh']]['letype'] == 'fern' && $_GET['link'] != 'ja')
{
$path = '/kunden/261105_71522/webseiten/dev.delst/admin/PDF/fern_downloads/'.$_GET['kursid'].'/'.$filename.'';
}
elseif($_SESSION['xle'][$_GET['idsh']]['letype'] == 'kurs' && $_GET['link'] == 'ja')
{
$path = 'http://ls.gdvz.de/'.$filename.'';
}
elseif($_SESSION['xle'][$_GET['idsh']]['letype'] == 'kurs' && $_GET['link'] != 'ja')
{
$path = '/kunden/261105_71522/webseiten/dev.delst/pdf/'.$_GET['kursid'].'/'.$filename.'';
}

$check = file_exists($path);
if( $_GET['link'] == 'ja')
$check = fopen($path, "r");

//echo $path;
if ($check) {
$mm_type="application/octet-stream"; // modify accordingly to the file type of $path, but in most cases no need to do so

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: " . $mm_type);
header("Content-Length: " .(string)(filesize($path)) );
header('Content-Disposition: attachment; filename="'.basename($path).'"');
header("Content-Transfer-Encoding: binary\n");

readfile($path); // outputs the content of the file

exit();
}

else
echo'Datei wurde nicht auf dem Server gefunden';
}

Если я удалю заголовок («Content-Length:». (строка) (размер файла ($ path))); загрузка работает, но PDF-файл поврежден и не может быть открыт.

Я также попробовал следующее:

if($_GET['funktion'] == 'downloadverwaltung')
{$filename = basename($dateiname);
$path = '/kunden/261105_71522/webseiten/admin/PDF/fern_downloads/'.$rowkurs['courseid'].'/'.$filename.''; // the file made available for download via this PHP file

if($_SESSION['xle'][$_GET['idsh']]['letype'] == 'fern' && $_GET['link'] == 'ja')
{
$path = 'http://ls.gdvz.de/'.$filename.'';
}
elseif($_SESSION['xle'][$_GET['idsh']]['letype'] == 'fern' && $_GET['link'] != 'ja')
{
$path = '/kunden/261105_71522/webseiten/dev.delst/admin/PDF/fern_downloads/'.$_GET['kursid'].'/'.$filename.'';
}
elseif($_SESSION['xle'][$_GET['idsh']]['letype'] == 'kurs' && $_GET['link'] == 'ja')
{
$path = 'http://ls.gdvz.de/'.$filename.'';
}
elseif($_SESSION['xle'][$_GET['idsh']]['letype'] == 'kurs' && $_GET['link'] != 'ja')
{
$path = '/kunden/261105_71522/webseiten/dev.delst/pdf/'.$_GET['kursid'].'/'.$filename.'';
}

//echo $path;
if (file_exists($path)) {
// $mm_type="application/octet-stream"; // modify accordingly to the file type of $path, but in most cases no need to do so

// 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: " . $mm_type);
// header("Content-Length: " .(string)(filesize($path)) );
// header('Content-Disposition: attachment; filename="'.basename($path).'"');
// header("Content-Transfer-Encoding: binary\n");

set_time_limit(0);
output_file($path, basename($path), 'application/octet-stream');

exit();
}

else
echo'Datei wurde nicht auf dem Server gefunden';
}

Путь правильный и файл находится на сервере. Так что загрузка работает ..

Что я делаю неправильно?

0

Решение

Вы можете попытаться увеличить предел памяти с ini_set ( 'memory_limit', '256M' );

if (file_exists ( $filepath )) {

header ( 'content-type: application/octet-stream' );
header ( 'content-Transfer-Encoding: Binary' );
header ( 'content-length: ' . filesize ( $filepath ) );
header ( 'content-disposition: attachment; filename=' . basename ( $filepath ) );

// setzt temporär die Grenze für den zur Vergügung stehenden Arbeitsspeicher hoch
ini_set ( 'memory_limit', '256M' );

ob_clean ();
ob_flush ();
flush ();

readfile ( $filepath );
}
else {
Throw new Exception ( "Die Datei $filepath konnte nicht gefunden werden" );
}
1

Другие решения

Проверьте здесь: Как сделать файл PDF доступным для скачивания по ссылке HTML?

header("Content-Type: application/octet-stream");

$file = $_GET["file"] .".pdf";
header("Content-Disposition: attachment; filename=" . urlencode($file));
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Description: File Transfer");
header("Content-Length: " . filesize($file));
flush(); // this doesn't really matter.
$fp = fopen($file, "r");
while (!feof($fp))
{
echo fread($fp, 65536);
flush(); // this is essential for large downloads
}
fclose($fp);
0