PHP PDF 与 blob 数据一起即时压缩


PHP PDF to zip on the fly with blob data

我想使用以下脚本将存储为 blob 数据的 pdf 文件添加到 ZIP 中。

我得到两个我不明白的错误

警告:file_put_contents(ATP语句201203.pdf) [函数.文件放置内容]:无法打开流:权限被拒绝

注意:ZipArchive::addFile() [ziparchive.addfile]:空字符串为 文件名

我不知道我做错了什么?

$TP_Month  =''.$_POST["tp_year"].''.$_POST["tp_month"].'';
$TP_format =$TP_Month;
echo $_POST["ids"];
$zip = new ZipArchive;
$zip->open('file.zip', ZipArchive::CREATE);
foreach( explode( ',', $_POST["ids"]) as $Client_ID)
{
  $sql_qry="select *
            from   ca_client_statement
            where  client_id='".$Client_ID."' and trading_period_month like '".$TP_Month."'";
  $sql_err_no=sql_select($sql_qry,$sql_res,$sql_row_count,$sql_err,$sql_uerr);
  $row = mysql_fetch_assoc($sql_res);
  $file_content = $row['pdf_statement'];
  $file_name = ''.$Client_ID.'statement'.$TP_format.'.pdf';
  $file=file_put_contents($file_name, $file_content);
  $zip->addFile($file);
}
$zip->close();
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename=filename.zip');
header('Content-Length: ' . filesize($zipfilename));
readfile($zipname);

"权限被拒绝"表示 Web 服务器没有写入您尝试保存文件的位置的权限。您必须从命令行(如果您使用的是 linux)使用chmod更改您尝试写入的目录的权限。

作为错误的证据,您可以执行chmod 777 /path/to/your/save/location,但是保持权限打开并不是一个好主意,您应该将其拨回644左右。

你会得到"空字符串作为文件名",因为file_get_contents()失败了(出现上述错误),当它失败时,它会返回布尔false(根据文档),这(显然)被解释为空字符串文件名。