3

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);
Rahil Wazir
  • 10,007
  • 11
  • 42
  • 64
nikmlnkr
  • 105
  • 2
  • 11

1 Answers1

5

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/";
Rahil Wazir
  • 10,007
  • 11
  • 42
  • 64
  • This solved the login connection problem but now I am not able to upload images :( The script just doesn't upload the images on server and there's no error log message as well! – nikmlnkr Apr 09 '14 at 17:38
  • Where are you working with this script in your remote host or localhost? If remote host your images path will not work because it will check them on remote server. Also make sure your FTP access has `/public_html/img` folder – Rahil Wazir Apr 09 '14 at 18:04
  • Oh, alright the code is working just fine with the remote server, however I want the code to fetch images from my 'upload_images' folder on my 'E' drive as mentioned in the folder. I am getting a number of images getting uploaded on the server but not the ones to which I have provided the path to on my PC. – nikmlnkr Apr 09 '14 at 18:24
  • Alright, give me 10 minutes, testing it out. And thanks for all the help btw. – nikmlnkr Apr 09 '14 at 19:37