fileExists() public method

Checks if file or directory exists.
public fileExists ( $file ) : boolean
return boolean
 /**
  * @param DatabaseBackupFile $file
  * @return ResultObject[]
  */
 public function process(DatabaseBackupFile $file)
 {
     $d = $file->getBackupDate();
     $results = [];
     foreach ($this->uploadsCredentials as $credentials) {
         $result = new ResultObject();
         // empty ResultObject means all is OK
         $backupPath = $credentials['path'] . '/' . $d->format('Y') . '/' . $d->format('F');
         $entireFilePath = $backupPath . '/' . $file->getFileName();
         try {
             $ftp = new \Ftp();
             $ftp->connect($credentials['host']);
             $ftp->login($credentials['username'], $credentials['password']);
             if (!$ftp->fileExists($backupPath)) {
                 $ftp->mkDirRecursive($backupPath);
             }
             $ftp->put($entireFilePath, $file->getFilePath(), FTP_BINARY);
             $ftp->close();
         } catch (\FtpException $e) {
             $this->logger->addCritical(sprintf('Uploading backup file\'s failed. %s', $e));
             $result->addError('Zálohu se nepodařilo nahrát na: ' . $credentials['host'], 'error');
         }
         $results[] = $result;
     }
     return $results;
 }
Esempio n. 2
0
 /**
  * Runs the FTP deploy task.
  *
  * @return Result The result of the task.
  */
 public function run()
 {
     $ftp = new \Ftp();
     // connect to the server
     try {
         if ($this->useSSL) {
             $ftp->sslConnect($this->host);
         } else {
             $ftp->connect($this->host);
         }
         $ftp->login($this->user, $this->password);
         // create the target directory if it does not exist
         $ftp->chdir('/');
         if (!$ftp->fileExists($this->targetDirectory)) {
             $this->printTaskInfo('Creating directory: ' . $this->targetDirectory);
             $ftp->mkDirRecursive($this->targetDirectory);
         }
         // get files from git if enabled
         if ($this->gitDiff) {
             $this->files($this->getGitDiff($ftp));
         }
         // scan and index files in finder
         $this->printTaskInfo('Scanning files to upload...');
         // add discrete files
         $this->finder->append(new \ArrayIterator($this->files));
         // directories first
         $this->finder->sortByType();
         // display summary before deploying
         $this->printTaskInfo(sprintf('Deploying %d files to "%s://%s@%s%s"...', $this->finder->count(), $this->useSSL ? 'ftps' : 'ftp', $this->user, $this->host, $this->targetDirectory));
         // upload each file, starting with directories
         foreach ($this->finder as $file) {
             $this->upload($ftp, $file);
         }
         // close the connection
         $ftp->close();
     } catch (\FtpException $e) {
         return Result::error($this, 'Error: ' . $e->getMessage());
     }
     // success!
     return Result::success($this, 'All files deployed.');
 }