I am trying to get ftp to work through php to move a file to another server (not on the same lan). I already have this separated so it will happen when certain conditions are meet. But I must be missing something. Here is the important parts of the script.

$ImgFile='../auci/incoming/'.$ImgFil;
				//echo $ImgFil;
				//echo $ImgFile;
				$fp = fopen($ImgFil, 'r');
				$conn_id = ftp_connect($ftp_server);
				$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
				if($login_result){
					echo "Login is good!";
				}
				ftp_pasv($conn_id, true);
				if (ftp_fput($conn_id, $ImgFile, $fp, FTP_ASCII)) {
    					echo "Successfully uploaded $ImgFil\n";
				} 
				else{
    					echo "There was a problem while uploading $ImgFil\n";
				}
				fclose($fp);
			        ftp_close($conn_id);

I end up with the same result "There was a problem while uploading PictureName".
Any help is appreciated.
FNP

    You might want to check and see if ftp_connect works or not.

    Does ftp_login work?

    what about ftp_pasv?

    Unfortunately, these ftp functions don't appear to offer much in the way of error reporting.

      ftp_connect and ftp_login works.

      I am not sure how to test for ftp_pasv.

      I am starting to wonder if maybe it is a permissions thing, but I just don't know. I will dig into that this weekend.

        Try reading the docs for [man]ftp_pasv[/man]. They say it returns TRUE on success or FALSE on failure.

          10 days later

          I hate to bump an old post, but I will be back at this today and tomorrow.

          I have checked ftp_connect and ftp_login and both return as good.

          I have checked the permissions and they seem to be fine.

          As always any help is appreciated.
          FNP

            Ok so here is the final code work. It works now. I did not have the proper FTP account set up. Fixed that, then I was trying to 'fopen' a file in a different directory. Once that was fixed it worked like a charm.

            $fp = fopen('../auci/incoming/'.$ImgFil, 'r');
            				$conn_id = ftp_connect($ftp_server);
            				//if($conn_id){
            					//echo "Conn is Good!";
            				//}
            				$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
            				//if($login_result){
            					//echo "Login is good!";
            				//}
            				if (ftp_fput($conn_id, $ImgFil, $fp, FTP_ASCII)) {
                					$UploadMessage="Successfully Uploaded Images!!!";
            				} 
            				else{
                					$UploadMessage="There was a problem while uploading images.";
            				}
            				ftp_close($conn_id);
            				fclose($fp);

            Thanks for the help.
            FNP

              Glad it worked out. Unfortunately, the ftp functions don't offer a lot of error reporting options.

              If I were you, I would consider checking for connection and login failures and reporting an error if either fails:

              if(!$login_result){
                // either die() here with an error message or write a log file or send yourself an email or something.
              } 

                That is a great idea. I will look into that over the next week.

                I just got done writing the code to delete the files from either server based on some specific criteria.

                Thanks for your help.
                FNP

                  Oh one last idea which might have helped you locate the problem. I believe most servers keep a log of FTP activity. If you had root-level access to the server, then you might be able to take a peek at that log to see the login failures. I think the file's location would vary by operating system and FTP server choice, but it would have been useful to help you realize that the login failure was your problem.

                  Another thing I was going to suggest was that you might consider using the login credentials to login manually via FTP -- either using an FTP client like FireFTP or via the command line interface on the server that is having the problems.

                  At any rate, you've got it sorted now. May it run consistently and reliably for you.

                    Write a Reply...