0

i use backpack and im trying to download zip that have Json of some query and images, inside. what i need is exactly like backpack backup. download after backuping is done. in my case, i need download when zipping is done.

zipping proses is in the download function in controller that called when user click "download"

  public function download($packId)
        {
            $content = DB::table('questions')                  
                         ->join('package_has_questions', 'questions.id', '=', 'package_has_questions.question_id')    
                         ->where('package_has_questions.package_id','=',$packId)
                         ->select('questions.*')          
                         ->get();

     $fileName = carbon::now()
     $data = json_encode($content);

     $files = glob(public_path('export/'.$fileName.'.json'));

        $zipper = new \Chumper\Zipper\Zipper;
        $zipper->make('export/'.$fileName.'.zip')->add($files);

        foreach($content as $eachContent){   
            $imgPathA=glob(public_path('img/game/'.$eachContent->img_a_file));
            $imgPathB=glob(public_path('img/game/'.$eachContent->img_b_file));
           // dd($imgPath);
            $zipper->folder('img')->add($imgPathA);
            $zipper->folder('img')->add($imgPathB);
        }
     $pathDownload =Response::download(public_path('export/'.$fileName.'.zip'))->deleteFileAfterSend(true);

    return $pathDownload;
}

this return an eror

FileNotFoundException in File.php line 37: 2016-10-25.zip" does not exist

but when i back and try to download it again, i can download it. and after that error file doesn't exist appear again

its look like, response of download is running before the zipping is done. in the second try, the response is download the zip that is already done from the first try.

if someone know some right way, i trully appreciate that Please Help, i really stuck in here

Frasaccordi
  • 29
  • 1
  • 4

1 Answers1

1

You need to close the zip before downloading it.

...
$zipper->close();
$pathDownload =Response::download(public_path('export/'.$fileName.'.zip'))->deleteFileAfterSend(true);
...