phpseclib\Net\SFTP::stat PHP Méthode

stat() public méthode

Returns an array on success and false otherwise.
public stat ( string $filename ) : mixed
$filename string
Résultat mixed
    function stat($filename)
    {
        if (!($this->bitmap & SSH2::MASK_LOGIN)) {
            return false;
        }
        $filename = $this->_realpath($filename);
        if ($filename === false) {
            return false;
        }
        if ($this->use_stat_cache) {
            $result = $this->_query_stat_cache($filename);
            if (is_array($result) && isset($result['.']) && isset($result['.']->stat)) {
                return $result['.']->stat;
            }
            if (is_object($result) && isset($result->stat)) {
                return $result->stat;
            }
        }
        $stat = $this->_stat($filename, NET_SFTP_STAT);
        if ($stat === false) {
            $this->_remove_from_stat_cache($filename);
            return false;
        }
        if (isset($stat['type'])) {
            if ($stat['type'] == NET_SFTP_TYPE_DIRECTORY) {
                $filename .= '/.';
            }
            $this->_update_stat_cache($filename, (object) array('stat' => $stat));
            return $stat;
        }
        $pwd = $this->pwd;
        $stat['type'] = $this->chdir($filename) ? NET_SFTP_TYPE_DIRECTORY : NET_SFTP_TYPE_REGULAR;
        $this->pwd = $pwd;
        if ($stat['type'] == NET_SFTP_TYPE_DIRECTORY) {
            $filename .= '/.';
        }
        $this->_update_stat_cache($filename, (object) array('stat' => $stat));
        return $stat;
    }

Usage Example

 /**
  * sync local directory with ftp directory
  *
  * @param string        $src
  * @param string        $dst
  * @param callable|null $syncop
  *
  * @throws GlSyncFtpException
  */
 public function syncDirectory($src, $dst, callable $syncop = null)
 {
     $this->login();
     $files = [];
     $dirs = [];
     $this->getFiles($dst, "", $files, $dirs);
     // delete on ftp server, files not present in local directory
     foreach ($files as $name => $raw) {
         if (!file_exists($src . $name)) {
             $filepathFtp = $dst . strtr($name, ["\\" => "/"]);
             if ($syncop) {
                 $syncop(self::DELETE_FILE, $filepathFtp);
             }
             $this->sftp->delete($filepathFtp);
         }
     }
     // delete on ftp server, unknowns directories
     $dirs = array_reverse($dirs);
     foreach ($dirs as $name => $raw) {
         if (!file_exists($src . $name)) {
             $filepathFtp = $dst . strtr($name, ["\\" => "/"]);
             if ($syncop) {
                 $syncop(self::DELETE_DIR, $filepathFtp);
             }
             $this->sftp->rmdir($filepathFtp);
         }
     }
     // create new directories
     $finderdir = new Finder();
     $finderdir->directories()->ignoreDotFiles(false)->followLinks()->in($src)->notName('.git*');
     /**
      * @var SplFileInfo $dir
      */
     foreach ($finderdir as $dir) {
         $dirpathFtp = $dst . "/" . strtr($dir->getRelativePathname(), ["\\" => "/"]);
         $stat = $this->sftp->stat($dirpathFtp);
         if (!$stat) {
             if ($syncop) {
                 $syncop(self::CREATE_DIR, $dirpathFtp);
             }
             $this->sftp->mkdir($dirpathFtp, $dir->getRealPath(), SFTP::SOURCE_LOCAL_FILE);
             $this->sftp->chmod(0755, $dirpathFtp, $dir->getRealPath());
         }
     }
     // copy new files or update if younger
     $finderdir = new Finder();
     $finderdir->files()->ignoreDotFiles(false)->followLinks()->in($src)->notName('.git*');
     /**
      * @var SplFileInfo $file
      */
     foreach ($finderdir as $file) {
         $filepathFtp = $dst . "/" . strtr($file->getRelativePathname(), ["\\" => "/"]);
         $stat = $this->sftp->stat($filepathFtp);
         if (!$stat) {
             if ($syncop) {
                 $syncop(self::NEW_FILE, $filepathFtp);
             }
             $this->sftp->put($filepathFtp, $file->getRealPath(), SFTP::SOURCE_LOCAL_FILE);
         } else {
             $size = $this->sftp->size($filepathFtp);
             if ($file->getMTime() > $stat['mtime'] || $file->getSize() != $size) {
                 if ($syncop) {
                     $syncop(self::UPDATE_FILE, $filepathFtp);
                 }
                 $this->sftp->put($filepathFtp, $file->getRealPath(), SFTP::SOURCE_LOCAL_FILE);
             }
         }
     }
 }
All Usage Examples Of phpseclib\Net\SFTP::stat