[英]How to check uploaded file is exists with SFTP using phpseclib
我正在使用phpseclib將文件上傳到遠程服務器。 以前,我的腳本工作正常,但我期望它可以檢查遠程服務器是否已收到該文件。 如文件存在則返回1 / true或文件不存在則返回0 / false。
我的代碼:
include('Net/SFTP.php');
$path = "upload/";
$path_dir .= $ref .".xml";
$directory = '/home/XML/';
$directory .= $ref .".xml";
$sftp = new Net_SFTP('xxx.com',22);
if (!$sftp->login('username', 'password'))
{
exit('SFTP Login Failed');
}
echo $sftp->pwd();
$sftp->put($directory, $path_dir, NET_SFTP_LOCAL_FILE);
if (!$sftp->get($directory))
{
echo "0";
}
文件上傳成功,但不知道如何檢查文件是否存在。
有沒有人對此進行故障排除有任何想法或建議?
您可以使用file_exists
方法:
if ($sftp->file_exists($directory))
{
echo "File exists";
}
( $directory
變量名在這里真的很混亂。它是文件而不是目錄的路徑。)
雖然它足以測試put
方法的結果。 不需要額外的檢查。
file_exists
方法僅適用於phpseclib 0.3.7。 如果您使用的是舊版本的phpseclib,請使用stat
方法:
if ($sftp->stat($directory))
{
echo "File exists";
}
無論如何, file_exists
內部調用stat
。
你也可以做$sftp->file_exists($directory)
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.