远程服务器上的ftp_get提取文件无法打开流:无此文件或目录

时间:2018-10-28 13:57:30

标签: php ftp

bottom

我在其他服务器上成功使用了上面的脚本,文件位于根文件夹中。但是在这一点上,我需要获取一个位于子目录中的文件(现在让我们说“文件夹名称”)。我尝试了不同的方法,但是都抛出了“无法打开流:没有这样的文件或目录”的错误。

我尝试过:

<?php
$local_file = 'filename';
$remote_file = '/folder name/filename';
$ftp_server ='IP';
$ftp_user_name = 'NAME';
$ftp_user_pass = 'PW';

// set up basic connection
$conn_id = ftp_connect($ftp_server);

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

// turn passive mode on
ftp_pasv($conn_id, true);

// upload a file
if (ftp_get($conn_id, $remote_file, $local_file, FTP_BINARY)) {
echo "successfully uploaded $local_file\n";
} else {
echo "There was a problem while uploading $local_file\n";
}

// close the connection
ftp_close($conn_id);
?>

这是我第一次处理ftp_get命令。有人可以帮我陷入困境吗...不知道我在做什么错

1 个答案:

答案 0 :(得分:-1)

对于那些可能遇到相同问题的人,我通过添加以下代码行(使用ftp_chdir函数)对其进行了梳理,并且效果很好:

// turn passive mode on
ftp_pasv($conn_id, true);

echo "Current directory: " . ftp_pwd($conn_id) . "\n";

// try to change the directory to somedir
if (ftp_chdir($conn_id, "Live Stock")) {
    echo "Current directory is now: " . ftp_pwd($conn_id) . "\n";
} else { 
    echo "Couldn't change directory\n";
}

// upload a file
if (ftp_get($conn_id, $remote_file, $local_file, FTP_BINARY)) {
echo "successfully uploaded $local_file\n";
} else {
echo "There was a problem while uploading $local_file\n";
}