laravel 如何在laravel 5中压缩文件夹?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45450209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:25:39  来源:igfitidea点击:

how to zip folder in laravel 5?

phplaravelziparchive

提问by needhelp

need some help for laravel zip file. i have a folder in the public folder and i would like to create a zip of that folder (temp_file folder) whenever the user clicks the button.

需要 Laravel zip 文件的一些帮助。我在公共文件夹中有一个文件夹,我想在用户单击按钮时创建该文件夹的 zip(temp_file 文件夹)。

public function testing()
{
    $public_dir = public_path('temp_file/');
    $zipFileName = 'myZip.zip';
    $zip = new ZipArchive;

    if ($zip->open($public_dir . '/' . $zipFileName, ZipArchive::CREATE) === TRUE) {
        $zip->addFile('file_path', 'file_name');
        $zip->close();
    }

    $headers = array('Content-Type' => 'application/octet-stream');

    $filetopath = $public_dir . '/' . $zipFileName; 
}

but it seems that it is not creating the zip file and i can't download it. Please need some help

但它似乎没有创建 zip 文件,我无法下载它。请需要一些帮助

回答by Dan

First of all: The identical comparison in the if condition triggers me.

首先: if 条件中的相同比较触发了我。

Many people may not know this but ZipArchive::addFile()and ZipArchive::close()also returning booleans to show their success (or failure). You should always check them because only the closemethod returns if the folder isn't writeable.

很多人可能不知道这一点,但ZipArchive::addFile()ZipArchive::close()还返回布尔展示自己的成功(或失败)。您应该始终检查它们,因为close如果文件夹不可写,则只有该方法返回。

Then you said that nothing downloads if you call the controller action. That's right. You didn't tell the program to stream something to the client. You just set two variables, one for headers? and the other one for the exact same file path used above to open the zip file.

然后你说如果你调用控制器动作就不会下载任何东西。这是正确的。您没有告诉程序将某些内容流式传输到客户端。你只设置了两个变量,一个用于标题?另一个用于与上面用于打开 zip 文件的完全相同的文件路径。

The following code is a working example(at least in a configured environment with correct folder permissions) how this procedure canwork and get you some "inspiration" for your task.

以下代码是一个工作示例(至少在具有正确文件夹权限的配置环境中)此过程如何工作并为您的任务提供一些“灵感”。

public function testing()
{
    // Create a list of files that should be added to the archive.
    $files = glob(storage_path("app/images/*.jpg"));

    // Define the name of the archive and create a new ZipArchive instance.
    $archiveFile = storage_path("app/downloads/files.zip");
    $archive = new ZipArchive();

    // Check if the archive could be created.
    if ($archive->open($archiveFile, ZipArchive::CREATE | ZipArchive::OVERWRITE)) {
        // Loop through all the files and add them to the archive.
        foreach ($files as $file) {
            if ($archive->addFile($file, basename($file))) {
                // Do something here if addFile succeeded, otherwise this statement is unnecessary and can be ignored.
                continue;
            } else {
                throw new Exception("File [`{$file}`] could not be added to the zip file: " . $archive->getStatusString());
            }
        }

        // Close the archive.
        if ($archive->close()) {
            // Archive is now downloadable ...
            return response()->download($archiveFile, basename($archiveFile))->deleteFileAfterSend(true);
        } else {
            throw new Exception("Could not close zip file: " . $archive->getStatusString());
        }
    } else {
        throw new Exception("Zip file could not be created: " . $archive->getStatusString());
    }
});

回答by Bibhudatta Sahoo

You have to do in Popper format .
Do Like this

你必须在 Popper 格式中做。
这样做

$File = 'path-ZipFile/fileName' . time() . '.zip';
$zip = new \ZipArchive();
$res = $zip->open($File, \ZipArchive::CREATE);

if ($res === TRUE) {
    $zip->addFile("fullpath/filename.extinction");
    $zip->close();
}

header("Content-Type: application/zip");
header("Content-Disposition: attachment; filename=\"" . basename($File) . "\"");
header("Content-Length: " . filesize($File));
header("Pragma: no-cache");
header("Expires: 0");
readfile($File);
header("Connection: close");

回答by Bhavin Solanki

If everything works for you, you need to check the permissionof each directoryat public_path(). The directory must have 0755or 0777permission to create and execute files. Please check your directory permission first.

如果一切为你工作,你需要检查权限每个目录public_path()。该目录必须具有07550777权限才能创建和执行文件。请先检查您的目录权限。