示例#1
0
文件: FTP.php 项目: rvanbaalen/bili
 /**
  * Quick upload mthod for a single file.
  *
  * @param string $sourceFile local file name
  * @param array $ftpSettings (path.uploads, host, username, password)
  * @throws \RuntimeException
  */
 public static function ftpUpload($sourceFile, $ftpSettings, $targetFile = null, $blnSecure = false)
 {
     $strFtpFileName = is_null($targetFile) ? basename($sourceFile) : $targetFile;
     $strFtpFileDir = $ftpSettings['path']['uploads'];
     $objFtp = new FTP($ftpSettings['host'], 21, 90, $blnSecure);
     $objRet = $objFtp->login($ftpSettings['username'], $ftpSettings['password']);
     if (!$objRet) {
         throw new \RuntimeException("Could not login to FTP server.", 404);
     }
     //*** Passive mode.
     $objFtp->pasv(true);
     //*** Create dealer folder.
     try {
         $objFtp->mksubdirs($strFtpFileDir);
     } catch (\Exception $ex) {
         //*** Ignore. The folder probably already exists.
     }
     //*** Transfer file.
     $objRet = $objFtp->nb_put($strFtpFileName, $sourceFile, FTP_BINARY);
     while ($objRet == FTP_MOREDATA) {
         // Continue uploading...
         $objRet = $objFtp->nb_continue();
     }
     if ($objRet != FTP_FINISHED) {
         //*** Something went wrong.
         throw new \RuntimeException("FTP transfer of {$strFtpFileName} interruppted.", 500);
     }
     //*** Remove local file.
     if (file_exists($sourceFile)) {
         @unlink($sourceFile);
     }
 }