Php TCPDF文件名问题
在php中,当我下载pdf文件时,我希望下载的pdf文件名的名称如下simon(07-10-2015)。pdf 谢谢试试这个Php TCPDF文件名问题,php,tcpdf,Php,Tcpdf,在php中,当我下载pdf文件时,我希望下载的pdf文件名的名称如下simon(07-10-2015)。pdf 谢谢试试这个 ob_start(); $file = '';//pdf path here downloadFile($file,'simon (07-10-2015).pdf','text/plain'); function downloadFile($file, $name, $mime_type='') { if(
ob_start();
$file = '';//pdf path here
downloadFile($file,'simon (07-10-2015).pdf','text/plain');
function downloadFile($file, $name, $mime_type='')
{
if(!is_readable($file)) die('File not found or inaccessible!');
$size = filesize($file);
$name = rawurldecode($name);
$known_mime_types=array(
"pdf" => "application/pdf",
"txt" => "text/plain",
"html" => "text/html",
"htm" => "text/html",
"exe" => "application/octet-stream",
"zip" => "application/zip",
"doc" => "application/msword",
"xls" => "application/vnd.ms-excel",
"ppt" => "application/vnd.ms-powerpoint",
"gif" => "image/gif",
"png" => "image/png",
"jpeg"=> "image/jpg",
"jpg" => "image/jpg",
"php" => "text/plain"
);
if($mime_type==''){
$file_extension = strtolower(substr(strrchr($file,"."),1));
if(array_key_exists($file_extension, $known_mime_types)){
$mime_type=$known_mime_types[$file_extension];
} else {
$mime_type="application/force-download";
};
};
@ob_end_clean();
if(ini_get('zlib.output_compression'))
ini_set('zlib.output_compression', 'Off');
header('Content-Type: ' . $mime_type);
header('Content-Disposition: attachment; filename="'.$name.'"');
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); //you may want to change this
$bytes_send = 0;
if ($file = fopen($file, 'r'))
{
if(isset($_SERVER['HTTP_RANGE']))
fseek($file, $range);
while(!feof($file) &&
(!connection_aborted()) &&
($bytes_send<$new_length)
)
{
$buffer = fread($file, $chunksize);
print($buffer);
flush();
$bytes_send += strlen($buffer);
}
fclose($file);
} else
die('Error - can not open file.');
die();
}
ob_start();
$file=''//pdf路径在这里
下载文件($file,'simon(07-10-2015).pdf','text/plain');
函数downloadFile($file,$name,$mime\u type='')
{
如果(!is_readable($file))死亡('找不到文件或无法访问!');
$size=filesize($file);
$name=rawurldecode($name);
$known\u mime\u types=数组(
“pdf”=>“应用程序/pdf”,
“txt”=>“文本/普通”,
“html”=>“文本/html”,
“htm”=>“文本/html”,
“exe”=>“应用程序/八位字节流”,
“zip”=>“应用程序/zip”,
“doc”=>“应用程序/msword”,
“xls”=>“应用程序/vnd.ms excel”,
“ppt”=>“应用程序/vnd.ms powerpoint”,
“gif”=>“图像/gif”,
“png”=>“图像/png”,
“jpeg”=>“图像/jpg”,
“jpg”=>“图像/jpg”,
“php”=>“文本/普通”
);
如果($mime_type==''){
$file_extension=strtolower(substr(strrchr($file,“.”),1));
如果(数组\密钥\存在($file\扩展名,$known\ mime\类型)){
$mime_type=$known_mime_type[$file_extension];
}否则{
$mime_type=“应用程序/强制下载”;
};
};
@ob_end_clean();
if(ini_get('zlib.output_compression'))
ini_集('zlib.output_compression','Off');
标题(“内容类型:”.$mime\U类型);
标题('Content-Disposition:attachment;filename=“.”.$name.'”);
标题(“内容传输编码:二进制”);
标题('Accept-Ranges:bytes');
标头(“缓存控制:专用”);
标题('Pragma:private');
标题(“到期日期:1997年7月26日星期一05:00:00 GMT”);
如果(isset($\u服务器['HTTP\u范围]]))
{
列表($a,$range)=分解(“=”,$\u服务器['HTTP\u range',2);
列表($range)=分解(“,”,$range,2);
列表($range,$range_end)=分解(“-”,$range);
$range=intval($range);
如果(!$range\u end){
$range_end=$size-1;
}否则{
$range\u end=intval($range\u end);
}
$new\u length=$range\u end-$range+1;
标题(“HTTP/1.1 206部分内容”);
标题(“内容长度:$new_长度”);
标题(“内容范围:字节$Range-$Range_end/$size”);
}否则{
$new_length=$size;
标题(“内容长度:.$size”);
}
$chunksize=1*(1024*1024);//您可能需要更改此值
$bytes\u send=0;
如果($file=fopen($file,'r'))
{
如果(isset($\u服务器['HTTP\u范围]]))
fseek($file,$range);
而(!feof($file)和
(!connection_aborted())&&
($bytes\u sendth必须在某个地方生成!!请参考:您是在询问tcpdf吗?然后您可以直接更改$pdf->输出('anyname.pdf',I'))
谢谢您的回复。这很有效,但文件下载后无法读取。但我通过另一种方式得到了解决方案,我只需更改tcpdf库中的一个小片段。如果($dest[0]!='F'){$name=preg_replace('/[\s]+/','.'.''''.'''.''.'''.''.''.$name=preg_replace('/[^a-zA-Z0-9.-]-]/',''.'.'.'.$name)}您只需对上述代码进行注释。它将以名称作为您的通行证。无论如何,谢谢。。