开发者

Error with connecting ftp through php

I'm trying to connect to my server using php script to upload some files...

But it doesn't connect...

I dont know what is the error...

I'm sure that ftp is enable, i checked it through php_info()

What may be the error...

<?php
error_reporting(E_ALL); 
$ftp_server = "server.com";  //address of ftp server (leave out ftp://)
$ftp_user_name = "Username"; // Username
$ftp_user_pass = "Password";   // Password

$conn_id = ftp_connect($ftp_server);        // set up basic connectio开发者_如何学运维n

$login_result = ftp_login($conn_id,$ftp_user_name,$ftp_user_pass);

if ($login_result = ftp_login($conn_id,$ftp_user_name,$ftp_user_pass)) {
    echo "Connected as ,$ftp_user_name,$ftp_user_pass \n";
} else {
    echo "Couldn't connect \n";
}
.....
.....
....
....
ftp_close($conn_id); // close the FTP stream
?>


maybe you have to turn on the passive mode by doing:

ftp_pasv($conn_id, true);

directly after your ftp_login

PS: why do you do a double login? write

$login_result = ftp_login($conn_id,$ftp_user_name,$ftp_user_pass);

if ($login_result) {

instead of

$login_result = ftp_login($conn_id,$ftp_user_name,$ftp_user_pass);

if ($login_result = ftp_login($conn_id,$ftp_user_name,$ftp_user_pass)) {


This looks wrong to me:

$login_result = ftp_login($conn_id,$ftp_user_name,$ftp_user_pass);

if ($login_result = ftp_login($conn_id,$ftp_user_name,$ftp_user_pass)) {

You should just need:

$login_result = ftp_login($conn_id,$ftp_user_name,$ftp_user_pass);

if ($login_result) {

Otherwise it will attempt to log in twice, this could be the issue.

Also try adding or die to the ftp_conect to see if it can even connect to server.

$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server"); 


  1. Check the server you're trying to connect actually accepts connections from wherever this script is running from by using a regular FTP client. Your code may be correct but the FTP server isn't accepting connections from your server, or there's a firewall blocking things.
  2. Most PHP functions will log error information internally that you can retrieve with error_get_last() and/or $php_errormsg. Some diagnostic information as to why the login call is failing may be stored in there.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜