Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Jan 2006
    Posts
    199
    Thanks
    30
    Thanked 0 Times in 0 Posts

    PHP FTP Transfer Issue

    I'm having problems with a simple PHP FTP script, it partially works, it connects fine and creates the file on the new FTP account, but I get a warning error and the file is empty.

    Warning: ftp_put() [function.ftp-put]: PORT command successful in /home/export.php on line 16

    Any idea what I'm doing wrong?

    PHP Code:

    $file 
    "filefolder/csvfile.txt"/*source file on the server which we wana download ,single file name refers that file is in Home/root*/
    $local_file "newfolder/csvfile.txt";//download file and store as local.tar
    //================================
    $sessid ftp_connect($server); //connect
    $login_ok ftp_login($sessid$user"$pass"); //login
    if ((!$sessid) || (!$login_ok)):
     echo 
    "failed to connect: check hostname, username & password";
     exit; 
    //failed? Unable to connect!
    endif;
     
    if (
    ftp_put($sessid$local_file$fileFTP_BINARY)) //Ftp get function which will download file
    {
     echo 
    "Successfully written to $local_file\n";
    } else {
     echo 
    "There was a problem\n";
    }
     
    ftp_close($sessid); 

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    $file is a string. It needs to be a resource. Run the file through an fopen and provide the third argument with that file resource. The variable names may be incorrect though; you are running a put call, but you are calling the file you are uploading as $file and the remote name will be $local_file. These seem backwards to me.

    If you still have issues, try running a ftp_pasv($sessid, true); first to run in passive mode.

  • #3
    Regular Coder
    Join Date
    Jan 2006
    Posts
    199
    Thanks
    30
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    $file is a string. It needs to be a resource. Run the file through an fopen and provide the third argument with that file resource. The variable names may be incorrect though; you are running a put call, but you are calling the file you are uploading as $file and the remote name will be $local_file. These seem backwards to me.

    If you still have issues, try running a ftp_pasv($sessid, true); first to run in passive mode.
    I tried that but if I do that I get:

    Warning: ftp_put() expects parameter 3 to be string, resource given

    And yea, the names are a bit backwards, I had taken this script from one that originally used the ftp_get function, but I just changed it to put instead.
    Last edited by Remix919; 01-14-2013 at 04:37 AM.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Oh yeah my bad you're right. I was thinking of ftp_fput, not ftp_put.
    Open the passive mode. I'm not that familiar with the possible errors from the ftp. This is definitely bizarre looking to me (it looks like it successfully did nothing :/).
    Make sure your filepath is valid as well for the local file. You can check that using file_exists. It'll need to be readable as well. Pull a list of the ftp_nlist as well to see if the directory for the file you want to upload is available. The documentation doesn't specify, but I don't think FTP allows you to create a directory implicitly with an upload.

  • #5
    Regular Coder
    Join Date
    Jan 2006
    Posts
    199
    Thanks
    30
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    Oh yeah my bad you're right. I was thinking of ftp_fput, not ftp_put.
    Open the passive mode. I'm not that familiar with the possible errors from the ftp. This is definitely bizarre looking to me (it looks like it successfully did nothing :/).
    Make sure your filepath is valid as well for the local file. You can check that using file_exists. It'll need to be readable as well. Pull a list of the ftp_nlist as well to see if the directory for the file you want to upload is available. The documentation doesn't specify, but I don't think FTP allows you to create a directory implicitly with an upload.
    What's the difference between fput and put? I think all the paths are correct because it successfully creates the file in the right directory, the problem is the file is empty :\ Tried the passive mode and same issue, warning message and just puts an empty file there.

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    I believe it will always attempt to create the file and then take the data within the source to copy. If the source isn't valid, it would result in an empty file.
    fput is using an open file, put is using a filename. I would use the fput since you can verify with 100% certainty that the file is readable after opening before moving it to the server.

  • #7
    Regular Coder
    Join Date
    Jan 2006
    Posts
    199
    Thanks
    30
    Thanked 0 Times in 0 Posts


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •