我想通过PHP从我的服务器下载PDF,也可以通过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:”。(字符串)(filesize($ 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 :(得分:2)
您可以尝试使用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 :(得分:0)
点击此处: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);