tarang19's avatar

laravel 7 File Not Found Exception on download zip file

I try to create zip file to download photo from database but giving an error

Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException The file "D:\nat_test\public\images.zip" does not exist

my Controller

public function photo(){
        $date_of_test = Cycledate::where('active',1)->first()->testdate;
        $image_records = Form::where('date_of_test',$date_of_test)->get();
        //dd($image_records->user->student->photo);
        $zip = new ZipArchive;
        $path = public_path('images');
        $fileName = 'images.zip';

        

        if ($zip->open(public_path($fileName), ZipArchive::CREATE) === TRUE)
        {
                
            $files = File::files($path);
              
            foreach ($image_records as $key => $value) {
                
                $img = basename($value->user->student->photo);
              
                $zip->addFile($value, $img);
            }
             
            $zip->close();
        }

  
    
        return response()->download(public_path($fileName));

    }
0 likes
2 replies
tarang19's avatar
tarang19
OP
Best Answer
Level 4

thanks for replay my issue solve issue is not related to path its about

 $files = File::files($path);

Now my updated code (working code)

public function photo(){
        $date_of_test = Cycledate::where('active',1)->first()->testdate;
        $image_records = Form::where('date_of_test',$date_of_test)->get();
        //dd($image_records->user->student->photo);
        $zip = new ZipArchive;
        $path = public_path($image_records);
        $fileName ='images.zip';

        //dd($fileName);

        if ($zip->open(public_path($fileName), ZipArchive::CREATE) === TRUE)
        {
                
            //$files = File::files($path);
              
            foreach ($image_records as $image_record) {
                 $files = public_path('images/'. $image_record->user->student->photo);
                //dd($value);
                $img = basename($files);
                //dd($img);
                //$relativeNameInZipFile = basename($value);
                //$zip->addFile($path . $img);
                $zip->addFile($files, $img);
            }
             
            $zip->close();

           //dd($files);
        }

        //dd($zip);

        //dd(response()->download(public_path($fileName)));
    
        return response()->download(public_path($fileName));

    }

Please or to participate in this conversation.