ZipArchive won't get all files

101 Views Asked by At

I am trying to create zip file from specific folder files, it does create the zip file but .env file (i guess any file that starts with dot) will not be include in zip files.

Question

How can I add all files from my folder to zip file regardless of their names?

code

public function downloadZip($fileName)
{
    $zip = new ZipArchive;
    $downloadFolder = 'downloads';
    if (!file_exists($downloadFolder))
    {
        mkdir($downloadFolder);
    }

    if ($zip->open(public_path($downloadFolder.'/'.$fileName.'.zip'), ZipArchive::CREATE) === TRUE)
    {
        $files = File::files(public_path($fileName));
        foreach ($files as $key => $value) {
            $relativeNameInZipFile = basename($value);
            $zip->addFile($value, $relativeNameInZipFile);
        }
        $zip->close();
    }
    return response()->download(public_path($downloadFolder.'/'.$fileName.'.zip'));
}

Screenshot

one

1

There are 1 best solutions below

2
Danilo Mercado Oudalova On

There is just something about it in this git commit.

Git commit of laravel

And in this place they debate about it.

Allow hidden files

You only need to add this to your code:

File::allFiles($directory, $hidden = true)