Advertisement
Guest User

php background process example (create zip archive)

a guest
Jun 20th, 2013
498
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.94 KB | None | 0 0
  1. <?php
  2.  
  3. class zip {
  4.   // Additional arguments passed to zip binary
  5.   protected $zipArgs;
  6.   // Source file or directory
  7.   protected $src;
  8.   // Destination file (the .zip file)
  9.   protected $dst;
  10.   // Process ID of the zip process
  11.   protected $pid=0;
  12.   // Console output of the zip process.
  13.   // NOTE: If the command is successfull the file will be deleted
  14.   protected $consoleOut;
  15.   // Success state cache
  16.   protected $success=null;
  17.  
  18.   // Constructor
  19.   function __construct($src, $dst, $zipArgs='') {
  20.     if (!file_exists($src))
  21.       throw new Exceprion("File ${file} does not exist");
  22.     elseif (file_exists($dst) && !is_writeable($dst))
  23.       throw new Exceprion("File ${dst} is not writeable");
  24.     elseif (!file_exists($dst) && !is_writeable(dirname($dst)))
  25.       throw new Exceprion("File ${dst} cannot be created");
  26.    
  27.     $this->src=$src;
  28.     $this->dst=$dst;
  29.   }
  30.  
  31.   // Execute the zip command
  32.   // The command wil build the zip and write its output to a tempfile
  33.   // If everything getting fine, the tempfile will be removed. This allows
  34.   // yo to check if the command was successfully.
  35.   // Return integer pid
  36.   public function zip() {
  37.     $this->success=null;
  38.     $this->consoleOut=tempnam(sys_get_temp_dir(), 'Zip');
  39.    
  40.     if (is_dir($this->src)) {
  41.       $this->zipArgs .= ' -r'; // rkursive pack dirs
  42.     }
  43.    
  44.     // echo $cmd;
  45.     $cmd = sprintf(
  46.       'zip %s %s %s > %s 2>&1 && rm -f %s > /dev/null 2>&1 & echo $!',
  47.       $this->zipArgs,
  48.       $this->dst,
  49.       $this->src,
  50.       $this->consoleOut,
  51.       $this->consoleOut
  52.     );
  53.    
  54.     return $this->pid = shell_exec($cmd);
  55.     }
  56.  
  57.  
  58.   // Blocking function, waiting until the zip command has finished
  59.   // return boolean success state
  60.   public function waitForFinish($ms=500) {
  61.     while(1) {
  62.       if (!$this->isRunning()) return $this->isSuccess();
  63.       // Reset time limit
  64.       set_time_limit(ini_get('max_execution_time'));
  65.       usleep($ms);
  66.     }
  67.   }
  68.  
  69.   // Check if the command is still running
  70.   // return boolean
  71.   public function isRunning()
  72.     {
  73.     $cmd=sprintf('ps -p %d > /dev/null 2>&1', $this->pid);
  74.     $exit=false;
  75.     system($cmd, $exit);
  76.     if ($exit === 0) return true;
  77.     else return false;
  78.     }
  79.  
  80.  
  81.   // Check if the command was successfull
  82.   // return boolean
  83.   public function isSuccess()
  84.     {
  85.     if ($this->success !== null)
  86.       return $this->success;
  87.     elseif ($this->pid === 0)
  88.       throw new Exception('No zip file created jet');
  89.     elseif ($this->isRunning())
  90.       return false;
  91.     else
  92.       return !file_exists($this->consoleOut);
  93.     }
  94.  
  95.  
  96.   // Get the the command output (stdout&stderr).
  97.   // File is only available on failure
  98.   public function getOutput() {
  99.     if ($this->pid === 0)
  100.       throw new Exception('No zip file created jet');
  101.     elseif (!file_exists($this->consoleOut))
  102.       return 'OK';
  103.     else
  104.       return file_get_contents($this->consoleOut);
  105.   }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement