Ftp_login expects parameter 1 to be a resource

I am trying to download some files from FTP and I have the following error:

Warning: ftp_login () expects parameter 1 to be a resource, boolean is listed in / home / content / 98/10339998 / html / upload.php on line 65 FTP connection encountered an error! Attempting to connect to thelegendmaker.net ....

caused by:

// set up a connection to ftp server $conn_id = ftp_connect("thelegendmaker.net"); // login with username and password $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); 

Does anyone know why this is happening? I tried using quotes, double quotes and single quotes, and no one works.

+6
source share
3 answers

The problem is that when ftp_connect() cannot connect to the FTP server, it returns FALSE instead of the resource link identifier usually returned. Check if your FTP server is alive using ping

you can do as

 if($conn_id){ // login with username and password $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); } 
+11
source

ftp_connect will return false if this fails. This will result in the error message you are experiencing instead of logging in.

I would recommend using a condition so as not to try to log in when your connection failed.

A couple of options that you have:

 // set up a connection to ftp server $conn_id = ftp_connect("thelegendmaker.net") or die("Unable to connect to server."); // login with username and password $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); 

or

 // set up a connection to ftp server $conn_id = ftp_connect("thelegendmaker.net"); // login with username and password if($conn_id !== false) $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); 

Since I get a response from your server to ping. I assume that you have configured your FTP server incorrectly.

+5
source

According to the manual,

Returns an FTP stream on success or FALSE on error.

So you can apply a simple filter:

 $conn_id = ftp_connect("thelegendmaker.net"); if (false === $conn_id) { throw new Exception("FTP connection error!"); } ... 
+2
source

All Articles