最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

PHP exec() - unzip large file - how to check if the process is finished? - Stack Overflow

programmeradmin4浏览0评论

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
Add a comment  | 

2 Answers 2

Reset to default 1

Unzip 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.

  1. Avoid execution time limits by adding:

    ini_set('max_execution_time', 0); 
    
  2. 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.

发布评论

评论列表(0)

  1. 暂无评论