【问题标题】:PHP - ftp_put failed to open stream?PHP - ftp_put 无法打开流?
【发布时间】:2014-07-12 05:14:30
【问题描述】:

我在远程主机上有一个网站。 我正在尝试让 php 页面通过 ftp 将文件上传到另一个远程服务器。

我要上传到的服务器有这些目录(都有777权限):

/
/public_html
/public_html/files

例如,这些是 ftp 详细信息:

host: example.com
user: user
pass: pass
port: 21

谁能帮我理解为什么这段代码不起作用:

$file = 'C:\Users\[my username]\Desktop\somefile.txt';
$remote_file = '/public_html/files/file.txt';

// set up basic connection
$conn_id = ftp_connect("example.com",21);

// login with username and password
$login_result = ftp_login($conn_id, "user", "pass");
echo $login_result;
// upload a file
if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) {
    echo "successfully uploaded $file\n";
} else {
    echo "There was a problem while uploading $file\n";
}

// close the connection
ftp_close($conn_id);

当我运行它时,我收到以下消息:

Warning: ftp_put(C:\Users\User\Desktop\somefile.txt) [function.ftp-put]: failed to open stream: No such file or directory in /home/user/public_html/page.php on line 11

非常感谢任何帮助,我已经疯狂地研究并且已经尝试了很多年。

【问题讨论】:

  • 当您使用 ftp_chdir() 更改到 '/public_html/files' 目录并使用 $remote_file='file.txt' 时会发生什么。它是否允许您更改目录?我对您的错误消息的解释是目录“/home/user/public_html/files/”在您将文件上传到的服务器上不存在。
  • 查看manual中“Kev”留下的评论,或许也值得关注。
  • 我按照你说的做了,更改为 '/public_html/files' 没有错误。尽管上传仍然失败并显示相同的错误消息,但我的猜测(可能是您的猜测)是它一定是不存在的本地文件,但它确实存在:/ ??
  • 我添加了这段代码来测试文件是否存在: if (file_exists($file)){ echo "file exists"; }else{ echo "找不到文件";无论文件路径如何(即使它存在),它都会返回 false,我是否声明路径错误?
  • 我可以确认您上一条评论中的代码对我有用。如果文件确实存在但没有显示,可能是权限有问题?也许尝试将文件的权限更改为 0666 并查看文件是否存在?

标签: php ftp


【解决方案1】:

$remote_file = 'files/file.txt'; 你应该试试这个

【讨论】:

    猜你喜欢
    • 2019-12-03
    • 2012-05-14
    • 2020-07-27
    • 2012-08-04
    • 1970-01-01
    • 2018-01-13
    • 2018-11-21
    • 1970-01-01
    相关资源
    最近更新 更多