A
A
Alexander2016-11-10 15:31:18
Laravel
Alexander, 2016-11-10 15:31:18

Unable to download a file from the server?

I use this module: https://github.com/Chumper/Zipper
Method:

public function getDownloadAllPassport($passports)
    {
        $passports = explode(',', $passports);
        $name = uniqid();
        foreach ($passports as $passport) {
            $user_name = VisaOrder::findOrFail($passport)->name;
            if (Upload::getFiles('passport', $user_name)) {
                File::copyDirectory("upload/passport/$user_name", "upload/zip/$name/$user_name");
            }
        }
        Zipper::make("upload/zip/$name/passport.zip")->add(glob("upload/zip/$name"));
        return response()->download(public_path("upload/zip/$name/passport.zip"));
    }

The method works almost as it should, ziguet and copies where necessary. But at the end, when you need to issue the file, it gives the following error:
FileNotFoundException in File.php line 37:
The file "/home/dragger/MyProjects/site.local/public/upload/zip/5824f3b11b986/passport.zip" does not exist

I decided to experiment by commenting out all the code in the method except return :
public function getDownloadAllPassport($passports)
    {
//        $passports = explode(',', $passports);
//        $name = uniqid();
//        foreach ($passports as $passport) {
//            $user_name = VisaOrder::findOrFail($passport)->name;
//            if (Upload::getFiles('passport', $user_name)) {
//                File::copyDirectory("upload/passport/$user_name", "upload/zip/$name/$user_name");
//            }
//        }
//        Zipper::make("upload/zip/$name/passport.zip")->add(glob("upload/zip/$name"));
        return response()->download(public_path("upload/zip/5824f3b11b986/passport.zip"));
    }

As soon as I download the file. I don't know why he doesn't see it the first time.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Ne-Lexa, 2016-11-10
@kentuck1213

Try another library, without too much fiddling with the file system.

public function getDownloadAllPassport($passports)
{
    $passports = explode(',', $passports);
    $name = uniqid();
    $zipFile = \PhpZip\ZipOutputFile::create();
    foreach ($passports as $passport) {
        $user_name = VisaOrder::findOrFail($passport)->name;
        if (Upload::getFiles('passport', $user_name)) {
            $recursive = true;
            $toZipPath = $name . '/';
            $zipFile->addDir("upload/passport/$user_name", $recursive, $toZipPath);
        }
    }
    $zipFile->outputAsAttachment('passport.zip');
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question