通过FTP问题上传文件

时间:2017-06-28 14:28:53

标签: php

我正在尝试使用PHP通过FTP上传文件。这是我的代码,它改编自here。连接失败,我收到ftp_login(): Login incorrect错误。我知道我的凭据是正确的,任何人都可以告诉我可能是什么问题或我如何调试它?

// FTP login details
$ftp_server='ftp.example.com'; 
$ftp_user_name='myuser'; 
$ftp_user_pass='mypw';
$file_path = '/filepath/';
$remote_file = $file_path.$remote_file;

// Create temporary file
$local_file=fopen('php://temp', 'r+');
fwrite($local_file, $file_string);
rewind($local_file);       

// FTP connection
$conn_id = ftp_connect($ftp_server);

// FTP login 
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

// check connection
if ((!$conn_id) || (!$login_result)) { 
    echo "FTP connection has failed!";
    echo "Attempted to connect to $ftp_server for user $ftp_user_name"; 
    exit; 
} else {
    echo "Connected to $ftp_server, for user $ftp_user_name";
}

// upload the file
$upload = ftp_put($conn_id, $remote_file, $local_file, FTP_BINARY); 

// check upload status
if (!$upload) { 
    echo "FTP upload has failed!";
} else {
    echo "Uploaded $source_file to $ftp_server as $destination_file";
}

// Close FTP connection
ftp_close($conn_id);

// Close file handle
fclose($local_file); 

0 个答案:

没有答案