0

我想问所以我有一个功能可以根据用户ID下载存储在文件夹中的所有文件(使用ZipArchive的zip格式)。

当我在本地、windows 或 cpanel 共享主机中运行 xampp 时,它可以工作。但是当我想将它移动到 Apache 和 Redhat 时,所有下载的文件都已损坏,zip 中的所有文件都变为 0 Kb。zip 中的所有文件名都正确,但大小为 0kb

这是我的代码

 $url = url('')."/storage/uploads/file/".$id_pra."/"; 
       //$url = "https://www.mbsb.insko.my/public/storage/uploads/file/".$id_pra."/"; 
        
        
            $zip = new \ZipArchive();

    $uploaddir = public_path().'/tmp'; 

        $tmp_file = tempnam($uploaddir,'');
    
        $zip->open($tmp_file, \ZipArchive::CREATE);
    
                foreach($files as $file){
                     $url2 = $url.$file->upload;
                     $url2 = str_replace(' ', '%20', $url2);

                        if (!function_exists('curl_init')){ 
                            die('CURL is not installed!');
                        }

                     $ch = curl_init();
                     curl_setopt($ch, CURLOPT_URL, $url2);
                     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

                     $output = curl_exec($ch);
                     curl_close($ch);
                     $download_file = $output;

                     $type = substr($url2, -5, 5); 
                     #add it to the zip
                     $zip->addFromString(basename($url.$file->upload),$download_file);
                }
                // close zip
                $zip->close();
     
                
               
                
                # send the file to the browser as a download
               ob_start();
              $strFile = file_get_contents($tmp_file);         

                header('Content-disposition: attachment; filename=DOC-'.$id_pra.'-'.'.zip');
                header('Content-type: application/zip');
                echo $tmp_file;
                  while (ob_get_level()) {
                    ob_end_clean();
                  }
chmod($tmp_file, 0755);
                  readfile($tmp_file);  
        
                exit;
                

4

0 回答 0