I am trying to create a zip from a large file in the directory. So, I have used PHP Cli with Ajax. I am using exec to run my command.
Check it out below:
$.ajax({
url: controller/compressToZip,
type: 'POST',
dataType: 'json',
data: {'path':'/var/www/html/sysAssets'},
error: function () {
console.log('error');
},
success: function (res) {
if(res) {
// Force to download
windows.location.href = res;
}
}
});
Cli
Here I am running detached command and store Zip file in temp folder
protected function runCli() {
$tempStorage = '/var/www/html/temp/var/zipFiles/';
$command = ' cd '.$tempStorage.' && nohup zip -r '.$filename.'.zip '. $path;
$command .= ' > /dev/null 2>&1 & echo $!';
$output = [];
exec($command, $output);
return ['path'=>$tempStorage.$filename.'.zip', 'process'=>(int)$output[0]];
}
Now its return the absolute path and process Id to Ajax response. But, when the file gets downloaded, Zip shows corrupted (Because detached command still running and zip is not completely created!).
So, I want to populate ajax call back when the detached command is fully excecuted (File zipped completely). Can anyone help with this? How I can compress the large files with Cli detached method?