Roc*_* ZZ 3 php sftp file-upload file-exists 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";
}
Run Code Online (Sandbox Code Playgroud)
文件上传成功,但不知道如何检查文件是否存在.
有没有人对此进行故障排除有任何想法或建议?
你可以使用file_exists方法:
if ($sftp->file_exists($directory))
{
echo "File exists";
}
Run Code Online (Sandbox Code Playgroud)
($directory变量名在这里真的很混乱.它是文件而不是目录的路径.)
虽然它足以测试该put方法的结果.不需要额外的检查.
该file_exists方法仅适用于phpseclib 0.3.7.如果您使用的是较旧版本的phpseclib,请使用以下stat方法:
if ($sftp->stat($directory))
{
echo "File exists";
}
Run Code Online (Sandbox Code Playgroud)
在file_exists内部调用stat反正.