コード例 #1
0
ファイル: Deployment.php プロジェクト: ondrs/ftp-deployment
 /**
  * Uploades files.
  * @return void
  */
 private function uploadFiles(array $files)
 {
     $root = $this->ftp->pwd();
     $root = $root === '/' ? '' : $root;
     $prevDir = NULL;
     $toRename = array();
     foreach ($files as $num => $file) {
         $remoteFile = $root . $file;
         $remoteDir = dirname($remoteFile);
         if ($remoteDir !== $prevDir) {
             $prevDir = $remoteDir;
             if (trim($remoteDir, '\\/') !== '' && !$this->ftp->isDir($remoteDir)) {
                 $this->ftp->mkDirRecursive($remoteDir);
             }
         }
         if (substr($remoteFile, -1) === '/') {
             // is dir?
             continue;
         }
         $localFile = $this->preprocess($orig = ".{$file}");
         if (realpath($orig) !== $localFile) {
             $file .= ' (filters was applied)';
         }
         $toRename[] = $remoteFile;
         $size = filesize($localFile);
         $retry = self::RETRIES;
         upload:
         $blocks = 0;
         do {
             $this->writeProgress($num + 1, count($files), $file, min(round($blocks * self::BLOCK_SIZE / max($size, 1)), 100));
             try {
                 $ret = $blocks === 0 ? $this->ftp->nbPut($remoteFile . self::TEMPORARY_SUFFIX, $localFile, Ftp::BINARY) : $this->ftp->nbContinue();
                 // Ftp::AUTORESUME
             } catch (FtpException $e) {
                 $this->ftp->reconnect();
                 if (--$retry) {
                     goto upload;
                 }
                 throw new Exception("Cannot upload file {$file}, number of retries exceeded.");
             }
             $blocks++;
         } while ($ret === Ftp::MOREDATA);
         $this->writeProgress($num + 1, count($files), $file);
     }
     foreach ($toRename as $num => $file) {
         $this->writeProgress($num + 1, count($toRename), "Renaming {$file}");
         $this->ftp->tryDelete($file);
         $this->ftp->rename($file . self::TEMPORARY_SUFFIX, $file);
         // TODO: zachovat permissions
     }
 }