【问题标题】:Uploading via FTP using PHP使用 PHP 通过 FTP 上传
【发布时间】:2015-06-20 05:46:38
【问题描述】:

我编写了一个脚本来将文件从我的计算机上传到外部服务器。但是我收到以下错误消息:

Warning: ftp_put(): Filename invalid

我曾尝试更改文件名等,但没有成功。 这是我的代码:

$ftp_server = "ipaddress";
$username = "username";
$password = "password";

$destination_file = 'C:\ftp_root\borna\ttt.txt';
$source_file = 'C:\inetpub\ftp_test.txt'; 

// setup basic connection
$conn_id = ftp_connect($ftp_server);

// check connection
if(!$conn_id){
    print "FTP connection has failed.<br>";
}else{
    print "FTP connection successfull.<br>";
}

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

// check login
if(!login_result){
    print "FTP login credentials not recognised.<br>";
}else{
    print "FTP login credentials recognised.<br>";
}

// upload our file
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY);


// read upload status
if(!$upload){
    print "FTP upload has failed.<br>";
}else{
    print "FTP upload was successfull on file $source_file to $destination_file via $ftp_server.<br>";
}

// close the ftp stream
ftp_close($conn_id);

【问题讨论】:

  • 您正在使用 FTP_BINARY 却试图上传一个 ASCII 文件。按照手册使用 FTP_ASCII php.net/manual/en/function.ftp-put.php
  • 嗯,所以要清楚。我什么时候应该用什么?如果您能形成一个答案,说明应该为哪些文件使用哪种传输模式,那就太好了。
  • 我已将其更改为 FTP_ASCII,但仍然出现相同的错误。
  • 您不需要转义文件名中的两个反斜杠吗? $source_file = "C:\inetpub\\ftp_test.txt";应该是 $source_file = "C:\\inetpub\\ftp_test.txt";或者你可以只使用单引号,然后不用担心转义反斜杠。
  • @ChipDean 试过了。仍然没有区别。

标签: php file-upload upload ftp


【解决方案1】:

我会说错误是正确的:

$destination_file = "C:\ftp_root\borna\ttt.txt";
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY);

您正在使用本地路径作为目标路径(请参阅C: 位)。

如果不确定服务器路径是什么样的,只需使用常规 FTP 客户端打开该站点即可。

如果您没有 FTP 客户端,您可以下载一个名为 FileZilla 的免费客户端。安装它,启动它,然后在顶部栏中输入您的 FTP 凭据(您在 $ftp_server$username$password 变量中拥有的数据)。建立连接后,您会在 Remote Site 框中看到远程路径。

【讨论】:

  • 对于目前一无所知的人来说,这真的是无益的答案。你能告诉我服务器路径应该是什么样子吗?
  • 如果他上传到windows服务器怎么办?
  • @ChipDean 顺便说一句,我是谁。
  • @Testy:请不要在这里称人们无益 - 这不是很有礼貌。我相信 Alvaro 正在努力提供帮助。
  • 即使您连接到 Windows 服务器,通过 FTP 看到的路径通常看起来不像 Windows 样式。这就是为什么@ÁlvaroG.Vicario 建议您连接 FTP 客户端并检查 FTP 服务器用于路径的语法。
猜你喜欢
  • 2011-05-19
  • 2010-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-08
  • 1970-01-01
  • 2017-05-01
相关资源
最近更新 更多