user1488434
user1488434

Reputation: 419

PHP PDF to zip on the fly with blob data

I want to use the script below to add pdf files stored as blob data to a ZIP.

I get two errors that I do not understand

Warning: file_put_contents(ATPstatement201203.pdf) [function.file-put-contents]: failed to open stream: Permission denied

and

Notice: ZipArchive::addFile() [ziparchive.addfile]: Empty string as filename

I do not know what I am doing wrong?

$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);

Upvotes: 2

Views: 2355

Answers (1)

nickb
nickb

Reputation: 59699

"Permission denied" means the web server doesn't have the permissions to write to the location you're trying to save the file to. You'll have to change the permissions to the directory that you're trying to write to using chmod from the command line (if you're on linux).

As a proof of what is wrong, you can do chmod 777 /path/to/your/save/location, however it isn't a good idea to keep the permissions that wide open, and you should dial it back to 644 or so.

And you're getting the "Empty string as filename" because file_get_contents() is failing (with the above error), and when it fails, it returns boolean false (as per the docs), which is (apparently) being interpreted as an empty string filename.

Upvotes: 3

Related Questions