Reputation: 3014
I would like to download a PDF via PHP from my server, which is also uploaded via PHP. Although it works with some files I get the following error message with individual files:
ERR_CONTENT_LENGTH_MISMATCH
This is my download function:
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';
}
If I remove header("Content-Length: " .(string)(filesize($path)) ); the download works, but the PDF file is then broken and can not be opened.
I also tried the following:
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';
}
The path is correct and the file is located on the server. So the upload works..
What am I doing wrong?
Upvotes: 0
Views: 1607
Reputation: 160
You could try to increase the Memory Limit with 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" );
}
Upvotes: 2
Reputation: 465
Check here: How to make PDF file downloadable in HTML link?
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);
Upvotes: 1