Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
358 views
in Technique[技术] by (71.8m points)

laravel download response downloads multiples folders

i want to make zip and download just 1 folder but laravel adds every files in every folder in that directory to zip file and then download it

here is my code:

 $license = license::findOrFail($id);
    $path = public_path() . '/Qr/subs/' . $license->system_code;

    $zip = new ZipArchive;
    $fileName = 'photos.zip';
    if ($zip->open(public_path($fileName), ZipArchive::CREATE) === TRUE) {
        $files = File::files(public_path("/Qr/subs/{$license->system_code}/"));

        foreach ($files as $key => $value) {
            $relativeNameInZipFile = basename($value);
            $zip->addFile($value, $relativeNameInZipFile);
        }
        $zip->close();
    }
    return response()->download(public_path($fileName));

and this is what my directory looks like:

enter image description here

problem is that i get my license from db and get all the files in folder(folder is named with license system_code) and add it to zip file and download it, but Laravel adds every file in every folder which are in Qr/subs to zip file and then download it

what i'm doing wrong???


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

it is solved!

i should have deleted last zip file first and then make new one.

because laravel adds file to existing zip file

so with this line File::delete(public_path('Qrs.zip')); i delete it at first line of my function and then build a new one


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...