How to add files to newly created folder in ziparchive using php?

前端 未结 2 1675
小蘑菇
小蘑菇 2021-01-16 11:35

\"enter

As in the image above ,I have images that are organized in virtual folders(in

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-16 12:07

    Thanks @dnagirl for your answer, but I was actually looking for a way to add files inside subfolders (maybe I was unable to make myself clear!)

    discovered that zipArchive automatically creates folder if we give 'path/to/filename' instead of just 'filename'

    This is how I achieved it in case anybody may need it!

    function zipFilesAndDownload($file_names,$archive_file_name,$img_localpath)
    {
      $folder_path=WSP_AK_PLUGIN_DIR ."akTempFolder/";
      $zip = new ZipArchive();
      if ($zip->open($folder_path.$archive_file_name, ZIPARCHIVE::CREATE )===TRUE) {
        //$zip->addEmptyDir('myimages');(this was what i thought would create folder!)
        foreach($file_names as $files)
        {
            //$zip->addFile($img_localpath.'/'.$files, 'myfolder/'.$files);
            $zip->addFile($img_localpath.'/'.$files['fileName'],    $files['folder_path'].$files['fileName']);
        }
        $zip->close();
    

    in above code $files['folder_path'] will give the path to file where it will be created For eg folder1/folder2/image1.jpg

        header("Content-type: application/zip"); 
        header("Content-Disposition: attachment; filename=$archive_file_name");
        header('Content-Type: application/force-download');
        header('Content-Length: ' . filesize($folder_path.$archive_file_name));
        header("Pragma: no-cache"); 
        header("Expires: 0");
        if( file_exists($folder_path.$archive_file_name)) {
            ob_clean();
            readfile($folder_path.$archive_file_name);
        }
        unlink($folder_path.$archive_file_name);
      }
    }
    

提交回复
热议问题