如何从图像base64 php创建zip

时间:2016-05-03 09:29:30

标签: javascript php ajax zip

var imgAlums = window.document.getElementsByClassName("imgAlu");
var photos= [];
for (var i = 0; i < imgAlums.length; i++) {
        photos.push(imgAlums[i].currentSrc);
}     
  

//填充照片数组
//照片[0] =   数据:图像/ ; BASE64,/ 9J / 4AAQSkZJRgABAQAAAQA ...;
  //照片[1] =数据:图像/
; BASE64,/ 9J / 4AAFWEJDKWMDCIWUEIOD ...

$.ajax({
           type: 'POST',
           url: '<?= URL_PROYECTO_SEGURO ?>/panel/assets/php/generateZipPhotos.php',
                    data: {"photos": JSON.stringify(photos)},
                    success: function (data) {
                        console.log(data);
                        alert("Operacion realizada con exito");
                    }
                });

PHP代码:

$nameZip = "test.zip";
$zip = new ZipArchive();
$zip->open($nameZip, ZipArchive::OVERWRITE);
$zip->addEmptyDir("Photos");
$photos= json_decode($_POST["photos"], true);

for ($i = 0; $i < count($photos); $i++) {

$data = $photos[$i];
$data = str_replace('data:image/png;base64,', '', $data);
$data = str_replace(' ', '+', $data);
$data = base64_decode($data);

$zip->addFromString("Photos/Photo" . $i . ".png", $data);
}

$zip->close();
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=$nameZip");
header("Content-Transfer-Encoding: binary");
readfile($nameZip);
unlink($nameZip);

但是没有正确生成zip文件。

[edit]生成了zip文件,但内部没有任何内容。问题是当您在zip文件中写入时。照片拼写错误

0 个答案:

没有答案