Error uploading images to server using ftp and php script, giving error on ftp_put() login

StackOverflow https://stackoverflow.com/questions/22967345

  •  30-06-2023
  •  | 
  •  

Question

I have a script written in php which basically uploads images to a server. I'm uploading this script onto my server using an ftp account credentials. However when I try to access it, it gives me login error. Here's the error log:

[09-Apr-2014 19:00:01 Asia/Kolkata] PHP Warning: ftp_login(): Sorry, cleartext sessions are not accepted on this server. in /home/gameeon/public_html/jts/local_upload.php on line 8 [09-Apr-2014 19:01:33 Asia/Kolkata] PHP Warning: ftp_login(): Sorry, cleartext sessions are not accepted on this server. in /home/gameeon/public_html/jts/local_upload.php on line 8 [09-Apr-2014 19:01:55 Asia/Kolkata] PHP Warning: ftp_login(): Sorry, cleartext sessions are not accepted on this server. in /home/gameeon/public_html/jts/local_upload.php on line 8 [09-Apr-2014 19:02:21 Asia/Kolkata] PHP Warning: ftp_login(): Sorry, cleartext sessions are not accepted on this server. in /home/gameeon/public_html/jts/local_upload.php on line 8 [09-Apr-2014 19:03:59 Asia/Kolkata] PHP Warning: ftp_login(): Sorry, cleartext sessions are not accepted on this server. in /home/gameeon/public_html/jts/local_upload.php on line 8

My code is as follows:

<?php
// connect and login to FTP server
$ftp_server = "ftp.gameeon.in"; // enter the ftp host name
$ftp_username = "temp@gameeon.in"; // put your ftp user name
$ftp_userpass = "********"; // put your ftp password

$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);

/// put the path of your image folder in your remote server i.e:
$ftp_upload_path="public_html/img/";

/// put the path of your image folder in your local machine:
$files = glob("E:/upload_images/*.*");

 foreach($files as $fl)
 {

     $fl_arr = explode("/",$fl); 
     $cn=count($fl_arr);
     $num=$cn-1;
     $file=$fl_arr[$num];

     // upload file
     // $fl = full path of localimages

     if (ftp_put($ftp_conn, $ftp_upload_path.$file,$fl, FTP_ASCII))
     {
         echo "Successfully uploaded $file. <br/>";
     }
     else
     {
         echo "Error uploading $file.<br/>";
     }
     sleep(3); /// pausing the code for 3 secs before next upload

 }

 // close connection
 ftp_close($ftp_conn);
?>

I'm getting an error on this line no. 8 which is:

$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
Was it helpful?

Solution

You need to use ftp_ssl_connect() function instead of ftp_connect() because server is using Explicit TLS/SSL

Try this:

$ftp_conn = ftp_ssl_connect($ftp_server)
            or die("Could not connect to $ftp_server");

If you are working on localhost you may encounter error saying

ftp_put(): I won't open a connection to 192...* something..

Then you have to use the ftp_pasv function.

As of docs:

In passive mode, data connections are initiated by the client, rather than by the server. It may be needed if the client is behind firewall.

In your case yes you need to use it you are using Explicit TLS/SSL just before your loop add this:

ftp_pasv($ftp_conn, true);

Also set your path public_html/img/ to /img/ or whatever directory exists:

$ftp_upload_path = "/img/";
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top