0

I want to ask So I have a function to download all files stored in the folder according to the user ID (in zip format using ZipArchive).

it works when i run in xampp locally, windows or cpanel shared hosting. but when i want to move it to Apache and Redhat, all downloaded files are corrupt, the all file inside zip become 0 Kb. All the file names in the zip are correct, but the size is 0kb

here is my code

 $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;
                

0 Answers0