ZipArchive在Laravel中不起作用 [英] ZipArchive does not work in laravel

查看:829
本文介绍了ZipArchive在Laravel中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有laravel项目,想添加用于压缩文件的功能.我正在使用php ZipArchive.当我尝试仅使用PHP创建ZIP文件时,我很幸运,但是当我尝试使用Laravel时,并未创建zip文件.

I have laravel project and whant to add feature for ziping files. I am using php ZipArchive. When I'm trying to create ZIP file using just PHP, I have luck, but when I'm trying with Laravel, zip files does not been created.

所以我要添加:使用ZipArchive; 而只是做:

So I have add: use ZipArchive; And just doing:

    $file_path = storage_path("creatives/helloworld.zip");

    $zip = new ZipArchive();
    $zip->open($file_path, ZipArchive::CREATE);

但是没有错误,也没有zip文件.你能给我什么建议?

But there is not error and no zip file. What can you advise me?

推荐答案

答复很晚,但这可能会对某人有所帮助.我也遇到了同样的问题,但是后来我选择使用"过程组件"并执行命令以创建zip. 如果您只关心zip的创建.您可以使用以下代码.

It is very late to reply but this may help someone. I too had this same issue but then I opted for using "Process Component" and execute the command to create zip. If you are only concerned with the zip creation. You can use the following code.

<?php 
$projectFolder = $destinationPath;
$zipFile = $nameOftheFile;

$process = new Process("zip -r $zipFile $projectFolder");
$process->run();

// executes after the command finishes
if (!$process->isSuccessful()) {
    // throw new ProcessFailedException($process);
    $response['msg'] = $process->getOutput();
}else{
    $response['msg'] = 'Build zipped';
}
return $response;

不要忘记安装zip扩展名.使用以下命令.

Don't forget to install zip extension. Use the below command.

sudo apt-get install zip

这篇关于ZipArchive在Laravel中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆