質問

I am needing to parse a series of php files to output .PDFs and .PNGs files before zipping them using zipArchive. What I would like to do is something like

$zip = new ZipArchive();
$zip->open($file, ZipArchive::OVERWRITE);

//If you access qr_gen.php on a browser it creates a QR PNG file.
$zip->addFile('qr_gen.php?criteria=1', 'alpha.png');
$zip->addFile('qr_gen.php?criteria=2', 'beta.png');
//If you access pdf_gen.php on a browser it creates a PDF file.
$zip->addFile('pdf_gen.php?criteria=A', 'instructions.pdf');

$zip->close();
header('Content-Type: application/zip');
header('Content-Length: ' . filesize($file));
header('Content-Disposition: attachment; filename="file.zip"');
readfile($file);
unlink($file);

This obviously does not work. How can I accomplish my goal?

役に立ちましたか?

解決

The following line will not work as you provide and url as filename:

$zip->addFile('qr_gen.php?criteria=1', 'alpha.png');

Instead you'll have to download the pngs first and store them locally. Then add them to the zip archive. Like this:

file_put_contents('alpha.png', 
    file_get_contents('http://yourserver.com/qr_gen.php?criteria=1');

$zip->addFile('alpha.png');

You'll find more information at the documentation page of ZipArchive::addFile()

他のヒント

What you will need to do is to get the files locally first. This can be (trivially) achieved using file_get_contents if you have URL fopen mappers set up, or failing that, cURL calls.

This is a sample way to do it:

$zip = new ZipArchive();
$zip->open("zipfile.zip",ZipArchive::OVERWRITE);
$URLs = array(
   "alpha.png" => "http://my.url/qr_gen.php?criteria=1",
   "beta.png" => "http://my.url/qr_gen.php?criteria=2",
   "instructions.pdf" => "http://my.url/pdf_gen.php?criteria=A");
foreach ($URLs as $file => $URL) {
  $f = @file_get_contents($URL);
  if (empty($f)) throw new Exception("File not found: ".$URL);
  $zip->addFromString($file, $f);
}

Your zip is then available as $zip for further processing.

First of all, execute all files into browser and put that content (png.pdf) into one folder and then create zip of it by fetching one by one.

hope it helps

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top