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.

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

报告相同问题?

悬赏问题

  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题