简体   繁体   English

下载php后如何将多张照片放在压缩文件中

[英]How to put multiple photos in a compressed file after downloading php

I have this code which downloads several images from private sites. 我有这段代码,可以从私人站点下载几张图片。 I want all these images to be placed in a zip file. 我希望所有这些图像都放在一个zip文件中。 how do I do this? 我该怎么做呢?

<?php
  $num = $_POST['num'];
  for ($i=1; $i <= $num ; $i++) { 
  $url_to_image = $_POST['img'].$i.'.jpg';
  $my_save_dir = "manga/" ;
  $filename = basename($url_to_image);
  $complete_save_loc = $my_save_dir . $filename;
  file_put_contents($complete_save_loc,
  file_get_contents($url_to_image));
  echo $i ."jpg". " /download" . '<br>';
   }
   ?>

You can use the php-ZipArchive class. 您可以使用php-ZipArchive类。 http://php.net/manual/de/zip.examples.php If you dont have compiled your php-interpreter with the option --enable-zip you can use otherwise the php shell execution functions (exec, shell_exec, passthru) to use the host-systems zip. http://php.net/manual/de/zip.examples.php如果未使用--enable-zip选项编译php-解释器,则可以使用其他php shell执行函数(exec,shell_exec,passthru)使用主机系统zip。

you can try ZipArchive like below 您可以尝试如下ZipArchive

$zip = new ZipArchive();

  $my_save_dir = "manga/files.zip";
  $zip->open($my_save_dir, ZipArchive::CREATE);

  $num = $_POST['num'];
  for ($i=1; $i <= $num ; $i++) { 
      $url_to_image = $_POST['img'].$i.'.jpg';

      $download_file = file_get_contents($url_to_image);
      $zip->addFromString(basename($url_to_image), $download_file);


   }

   $zip->close();
   echo $my_save_dir;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM