I use exec() function to unzip large file (when I tried using ZipArchive, I got an error with max execution time). Here is my code:
exec('unzip '.$path.'exports/'.$dir.'/'.$file.' -d '.$path.'exports/'.$dir.'_work/');
die('UNPACKING');
Now, I need to check if this proccess is finished, using another PHP script file. How I can do it?
I use exec() function to unzip large file (when I tried using ZipArchive, I got an error with max execution time). Here is my code:
exec('unzip '.$path.'exports/'.$dir.'/'.$file.' -d '.$path.'exports/'.$dir.'_work/');
die('UNPACKING');
Now, I need to check if this proccess is finished, using another PHP script file. How I can do it?
Share Improve this question asked Mar 25 at 15:51 user3022527user3022527 2,2082 gold badges16 silver badges12 bronze badges 3- 2 Unzip into a different filename, and rename it when done. Then have the other script check for the existence of the renamed file. Renaming is atomic so you won't see a partial file. – Barmar Commented Mar 25 at 16:13
- @Barmar can you show any example? I don't understand exactly how it would work. – user3022527 Commented Mar 25 at 17:56
- Not sure it can, help, but you can get the size of final unzipped file with -l option and then track the size of the partially extracted file – Tuckbros Commented Mar 25 at 22:33
2 Answers
Reset to default 1Unzip into a temporary directory, then rename it to the intended directory that the other script is looking for.
exec('unzip '.escapeshellarg($path.'exports/'.$dir.'/'.$file).' -d '.escapeshellarg($path.'exports/'.$dir.'_work.temp/'));
rename('.$path.'exports/'.$dir.'_work.temp', '.$path.'exports/'.$dir.'_work');
die('UNPACKING');
Note that this only works if the destination directory doesn't already exist, not for extracting into an existing directory.
Avoid execution time limits by adding:
ini_set('max_execution_time', 0);
Run the extraction process in the background, using Laravel Queue or another worker. Avoid using system functions (
exec()
,shell_exec()
), as they can cause security issues and lack portability.