2

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?

jedrzej.kurylo
  • 39,591
  • 9
  • 98
  • 107
Lusine Martirosyan
  • 79
  • 1
  • 5
  • 14
  • Try returning your zip archive maybe? – DevK Apr 05 '17 at 07:49
  • I think you're creating ZipArchive in file with some namespase. So in this case you have to use absolute path to class name: $zip = new \ZipArchive(); – Alex Slipknot Apr 05 '17 at 07:54
  • There's no error or zip file is highly unlikely. It's going to be either one or the other. However some zip operations don't throw exceptions, they just return `false` or an error code so you might what to keep an eye out. For example `open` returns either `true` (on success) or an error code. – apokryfos Apr 05 '17 at 07:55
  • Thanks for your response @devk. It's returning object:{ ["status"]=> int(0) ["statusSys"]=> int(0) ["numFiles"]=> int(0) ["filename"]=> string(0) "" ["comment"]=> string(0) "" } – Lusine Martirosyan Apr 05 '17 at 07:58
  • When I run this code, it returns "File was created", but it hadn't been created: if($zip->open($file_path, ZipArchive::CREATE) === true) { echo 'File was created'; } else{ echo 'Failed'; } – Lusine Martirosyan Apr 05 '17 at 08:09
  • 2
    The code you've posted won't create a zip file ___on disk___, just an "in memory" zip resource; it requires a `save()` call to actually create the file that (preferably after adding some files to the zip) – Mark Baker Apr 05 '17 at 08:09
  • @MarkBaker you were right. The reason is I should add file to zip folder or save it to create file. Thanks – Lusine Martirosyan Apr 05 '17 at 09:26
  • I created a Composer package some time ago to aid in handling zip and base64 encoded files and email attachments. It's not my best work, but it did serve me well in an enterprise environment, so you're welcome to use it or dissect it for inspiration. It uses a Provider to apply a custom plugin to the Storage service. https://github.com/kmuenkel/file-mutations/tree/master/src/Plugins – kmuenkel Mar 02 '20 at 07:00

1 Answers1

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
VishalParkash
  • 490
  • 3
  • 15