V
V
Vladimir Simanov2017-03-20 15:53:49
PHP
Vladimir Simanov, 2017-03-20 15:53:49

How to save several pictures in a folder in an archive using PHP?

It is necessary to push several images (from 1 to n) into an archive created through the ZipArchive class. The archive itself with the excel file is created correctly. When I start writing images to the archive, it does not create the archive explicitly, uploading a broken file of 1 byte.

if($res === TRUE){
                $zip->addFile($file_1);
                $zip->addFile($file_2);
                $zip->addEmptyDir('photo_drivers');
                for($l=0;$l<count($img_array);$l++){
                        
                        $zip->addFile($img_array[$l][key($img_array[$l])]['photo'][0],"photo_drivers/".key($img_array[$l]).".jpg");
                }
                $zip->close();
                return $name_zip;
              } else {
                 return "error ".$res;
              }

UPD: Breaks the archive because of the images. But what's the reason is that he doesn't want to add images in a loop. Adds one photo from the array without problems.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel, 2017-03-20
@PavelFokeev

Check the paths

$currentFile = $img_array[$l][key($img_array[$l])]['photo'][0];
if(file_exists($currentFile)) 
  $zip->addFile($currentFile,"photo_drivers/".key($img_array[$l]).".jpg");
else
  echo "file $i not found <br/>";

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question