duanraotun1674 2015-07-24 09:52 采纳率: 100%
浏览 57
已采纳

在php中创建zip并下载无法在Mozilla Browser中运行

I am trying to create a zip file and download it by PHP script using ZipArchive library.

$post['files'] This variable contains URL of the images which are hosted on Amazon S3 so first I am downloading them to my server, creating zip file and download them.

My current code is working fine with the chrome also i tested on Safari and Opera, its working fine but when I am trying to do the same thing with Mozilla Firefox, its now working.

Please have a look at my php script

       &lt?php 
       if(isset($_POST['createZip'])){
        $post = $_POST;   
        $file_folder = "ZipFIle"; // folder to load files
        //downloading on my server
            foreach($post['files'] as $file){
            $ch = curl_init($file);
            $fp = fopen($file_folder.basename($file), 'wb');
            curl_setopt($ch, CURLOPT_FILE, $fp);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_exec($ch);
            curl_close($ch);
            fclose($fp); 
        }
      if(extension_loaded('zip')){  // Checking ZIP extension is available
        if(isset($post['files']) and count($post['files']) > 0){  
         // Checking files are selected
          $zip = new ZipArchive();      // Load zip library 
          $zip_name = "ZipFIle.zip";      // Zip name
          if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE){  
          // Opening zip file to load files
            $error .=  "Sorry ZIP creation failed at this time";
          }
          $dir = new DirectoryIterator("ZipFIle"); 
          foreach ($dir as $fileinfo) {
            if (!$fileinfo->isDot()) {
                //var_dump($fileinfo->getFilename());
                $imgfile = $fileinfo->getFilename();
                foreach($post['files'] as $file){
                  if ( basename($file) ==  $imgfile) {
                    //get file and add it to zip
                    $zip->addFile($file_folder.$imgfile);// Adding files into zip
                  }
                }//foreach($post['files'] as $file)
              }//if
          }        
          $zip->close();
          if(file_exists($zip_name)){
            //echo $zip_name ;
            // push to download the zip
            header('Content-type: application/zip');
            header('Content-Disposition: attachment; filename="'.$zip_name.'"');
            readfile($zip_name);
            // remove zip file is exists in temp path
            unlink($zip_name);
            foreach ($dir as $fileInfo) {
                if(!$fileInfo->isDot()) {
                    //delete all files after creating zip
                    unlink($fileInfo->getPathname());
                }
            }//foreach for delete
          }else{echo "no zip";}
        }else
          $error .= "Please select file to zip";
      }else
        $error .= "You dont have ZIP extension";
    }
    ?&gt

I am getting these warnings when I am trying to execute my code

Warning: fopen(ZipFIle/) [function.fopen]: failed to open stream: Is a directory in /home/my-server/download.php on line 9

Warning: curl_setopt(): supplied argument is not a valid File-Handle resource in /home/my-server/download.php on line 10

Warning: fclose(): supplied argument is not a valid stream resource in /home/my-server/download.php on line 14

Warning: Cannot modify header information - headers already sent by (output started at /home/my-server/download.php:9) in /home/my-server/download.php on line 44

Warning: Cannot modify header information - headers already sent by (output started at /home/my-server/download.php:9) in /home/my-server/download.php on line 45

Kindly, guide!

  • 写回答

1条回答 默认 最新

  • dpwle46882 2016-02-03 13:08
    关注

    These messages are all server warnings. The browser is not relevant.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序