ZipArchive does not work in laravel

感情迁移 提交于 2020-03-22 09:40:09

问题


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.

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);

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


回答1:


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;

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

sudo apt-get install zip


来源:https://stackoverflow.com/questions/43225007/ziparchive-does-not-work-in-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!