问题
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