使用PHP通过FTP上传

时间:2015-04-14 14:42:23

标签: php file-upload upload ftp

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

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);

1 个答案:

答案 0 :(得分:0)

我说错误是正确的:

$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变量中的数据)。建立连接后,您将在远程站点框中看到远程路径。