我需要解析一系列php文件,以在使用zipArchive壓縮它們之前輸出.PDF和.PNG文件。我想要做的是類似由PHP文件生成的Zip文件
$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);
這顯然不起作用。我怎樣才能實現我的目標?
$zip->addFile('qr_gen.php?criteria=1', 'alpha.png');
相反,你必須先下載PNG圖像,並將它們存儲在本地:當您提供和URL作爲文件名
此外,其他的東西給了我整個上午。由於我使用了for()循環,並且在for循環結束時取消了鏈接() - 臨時圖片,所以沒有寫入我的zip文件。我必須在循環開始時使用'$ zip-> open($ file)'並在'unlink($ temp_pic_file)之前使用'$ zip-> close()'' – Andrew 2013-04-04 15:14:33
這很完美。但是,由於apache需要WRITE權限,我使用了具有770權限的'cache'子目錄'file_put_contents(「cache/alpha.png」,file_get_contents(「http://mydomain.com/qr_gen.php?ccriteria=1」)) ; $ zip-> addFile(「cache/alpha.png」,「alpha.png」);'另外,我將緩存目錄中創建的文件取消鏈接()。謝謝! – Andrew 2013-04-04 16:43:05
好點。我應該提到這一點。 – hek2mgl 2013-04-04 16:53:18