下载pdf脚本 - 在大PDF时重命名corupt文件

时间:2015-08-18 14:00:24

标签: php pdf download

在我的下载脚本中,如果大于50mb,则生成特定pdf的重写名称时出现问题。下载的文件是corupt!

我目前使用网址重写为3种不同语言生成3个不同的名称,并将上传文件的原始名称(id.pdf)替换为产品描述名称。

这是最终网址的示例:

/lang/download/manual/brand/product_name/type_of_manual/id.html

的.htaccess

RewriteRule ^(fr|en|es)/telecharger/manuel/(.*)/(.*).html$ telecharger.php?&DID=$3&title=$2 [QSA]
RewriteRule ^(fr|en|es)/download/manual/(.*)/(.*).html$ telecharger.php?&DID=$3&title=$2 [QSA]
RewriteRule ^(fr|en|es)/descargar/manual/(.*)/(.*).html$ telecharger.php?&DID=$3&title=$2 [QSA]

这是我的php脚本:

telecharger.php

    <?php

include("host/config.php");
include("host/functions/import.php");

function output_file($doc_name, $title)
{
if(!is_readable($doc_name)) die('File not found or inaccessible!');
$size = filesize($doc_name);
$title = str_replace(array('_', "-", "/"), array(" ", " ", " "), $title);
$title = rawurldecode($title);
@ob_end_clean(); 
if(ini_get('zlib.output_compression'))
ini_set('zlib.output_compression', 'Off');
header("Content-Type: application/pdf");
header('Content-Disposition: attachment; filename="'.$title.'"');
header("Content-Transfer-Encoding: binary");
header('Accept-Ranges: bytes');
header("Cache-control: private");
header('Pragma: private');
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
if(isset($_SERVER['HTTP_RANGE']))
{
list($a, $range) = explode("=",$_SERVER['HTTP_RANGE'],2);
list($range) = explode(",",$range,2);
list($range, $range_end) = explode("-", $range);
$range=intval($range);
if(!$range_end) {
$range_end=$size-1;
} else {
$range_end=intval($range_end);
}
$new_length = $range_end-$range+1;
header("HTTP/1.1 206 Partial Content");
header("Content-Length: $new_length");
header("Content-Range: bytes $range-$range_end/$size");
} else {
$new_length=$size;
header("Content-Length: ".$size);
}
$chunksize = 1*(1024*1024); 
$bytes_send = 0;
if ($doc_name = fopen($doc_name, 'r'))
{
if(isset($_SERVER['HTTP_RANGE']))
fseek($doc_name, $range);
while(!feof($doc_name) && 
(!connection_aborted()) && 
($bytes_send<$new_length)
)
{
$buffer = fread($doc_name, $chunksize);
print($buffer); 
flush();
$bytes_send += strlen($buffer);
}
fclose($doc_name);
} else
die('Error - can not open file.');
die();
}       
$DID = $_REQUEST['DID'];
$title = $_REQUEST['title'];
if (is_numeric($DID))
{
    $query="SELECT allowdownloads,doc_name FROM docs WHERE public='1' AND active='1' AND DID='".mysql_real_escape_string($DID)."' limit 1";
    $executequery = $conn->Execute($query);
    $allowdownloads = $executequery->fields['allowdownloads'];
    $doc_name = $executequery->fields['doc_name'];
}
set_time_limit(0);
$doc_name_path='ddata/'.$doc_name;

if ($allowdownloads == "1")
{
            $query="UPDATE docs SET downloadcount=downloadcount+1 WHERE DID='".mysql_real_escape_string($DID)."'";
            $conn->Execute($query);
            output_file($doc_name_path, ''.$title.'.pdf', 'application/pdf');
            exit;
}
?>

有人知道如何修复?使这个脚本更好的其他方法?

提前谢谢

1 个答案:

答案 0 :(得分:0)

感谢Isaac,强制内存限制为-1修复问题:)

  

可能是内存限制,请尝试ini_set('memory_limit',' - 1'); -   艾萨克

最终的代码是

<?php

include("host/config.php");
include("host/functions/import.php");
ini_set('memory_limit', '-1');
function output_file($doc_name, $title)
{
if(!is_readable($doc_name)) die('File not found or inaccessible!');
$size = filesize($doc_name);
$title = str_replace(array('_', "-", "/"), array(" ", " ", " "), $title);
$title = rawurldecode($title);
@ob_end_clean(); 
if(ini_get('zlib.output_compression'))
ini_set('zlib.output_compression', 'Off');
header("Content-Type: application/pdf");
header('Content-Disposition: attachment; filename="'.$title.'"');
header("Content-Transfer-Encoding: binary");
header('Accept-Ranges: bytes');
header("Cache-control: private");
header('Pragma: private');
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
if(isset($_SERVER['HTTP_RANGE']))
{
list($a, $range) = explode("=",$_SERVER['HTTP_RANGE'],2);
list($range) = explode(",",$range,2);
list($range, $range_end) = explode("-", $range);
$range=intval($range);
if(!$range_end) {
$range_end=$size-1;
} else {
$range_end=intval($range_end);
}
$new_length = $range_end-$range+1;
header("HTTP/1.1 206 Partial Content");
header("Content-Length: $new_length");
header("Content-Range: bytes $range-$range_end/$size");
} else {
$new_length=$size;
header("Content-Length: ".$size);
}
$chunksize = 1*(1024*1024); 
$bytes_send = 0;
if ($doc_name = fopen($doc_name, 'r'))
{
if(isset($_SERVER['HTTP_RANGE']))
fseek($doc_name, $range);
while(!feof($doc_name) && 
(!connection_aborted()) && 
($bytes_send<$new_length)
)
{
$buffer = fread($doc_name, $chunksize);
print($buffer); 
flush();
$bytes_send += strlen($buffer);
}
fclose($doc_name);
} else
die('Error - can not open file.');
die();
}       
$DID = $_REQUEST['DID'];
$title = $_REQUEST['title'];
if (is_numeric($DID))
{
    $query="SELECT allowdownloads,doc_name FROM docs WHERE public='1' AND active='1' AND DID='".mysql_real_escape_string($DID)."' limit 1";
    $executequery = $conn->Execute($query);
    $allowdownloads = $executequery->fields['allowdownloads'];
    $doc_name = $executequery->fields['doc_name'];
}
set_time_limit(0);
$doc_name_path='ddata/'.$doc_name;

if ($allowdownloads == "1")
{
            $query="UPDATE docs SET downloadcount=downloadcount+1 WHERE DID='".mysql_real_escape_string($DID)."'";
            $conn->Execute($query);
            output_file($doc_name_path, ''.$title.'.pdf', 'application/pdf');
            exit;
}
?>