使用 PHP 通过 FTP 上传

Uploading via FTP using PHP

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

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

我认为错误是正确的:

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