File Upload using ftp commands

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David

    File Upload using ftp commands

    Because of limitations of my provider, I am forced to use the ftp
    functions to upload files to my server (the permissions on the server
    will not allow me to load files via php).

    I have successfully ported the sample code from php.net to do everything
    BUT upload the file. I can connect. I can log in. I can change
    directories. But the file never uploads. As I note in the code, I have
    tried using ftp_put with the local filename, and I have tried using
    ftp_fput with the file in /tmp/php with no luck. Suggestions are welcome.

    David

    Code:
    ---------------------
    <?php
    $tmp = $_FILES['loadfile']; // passed in correctly as shown on next line
    echo "<pre>" . print_r($tmp) . "</pre>";
    
    $upload_form_file = $tmp['name']; // have tried using the tmp_name
    element combined with ftp_fput
    $dest_file = "bob.gif"; // just a name to try
    
    // set up a connection or die
    $conn_id = ftp_connect($ftp_server) or die("Couldn't connect to
    $ftp_server"); // works
    
    // try to login
    if (@ftp_login($conn_id, $ftp_user, $ftp_pass)) {
    echo "<p>Connected as $ftp_user@$ftp_server</p>\n"; // this is printed
    } else {
    echo "Couldn't connect as $ftp_user\n";
    }
    
    echo "<p>Current directory is now: " . ftp_pwd($conn_id) . "</p>\n"; //
    this gives the root
    
    if (ftp_chdir($conn_id, $base_path)) {
    echo "<p>Current directory is now: " . ftp_pwd($conn_id) . "</p>\n";
    // this prints /the/path/to/docs
    } else {
    echo "Couldn't change directory\n";
    }
    
    echo "<p>upload_form_file: $upload_form_file <br>\ndest_file: $dest_file
    </p>\n"; // files are listed
    
    if (ftp_put($conn_id, $dest_file, $upload_form_file, FTP_BINARY)) {
    echo "successfully uploaded";
    } else {
    echo "There was a problem while uploading $file\n"; // this always
    triggers
    }
    
    // close the connection
    ftp_close($conn_id);
    
    ?>
  • David

    #2
    Re: File Upload using ftp commands

    Dude:

    Here I am replying to myself on the list so that others won't suffer as
    I have. I finally figured out that ftp_put and ftp_fput are running *on
    the server* and so don't know anything about the client's file system.
    They are putting files from the php server onto some other remote
    server. Therefore, the only file they are going to know about is the
    temp file that php creates on the fly. Once I changed my code to use the
    php tmp file, the upload worked smoothly.

    HTH,
    David

    David wrote:[color=blue]
    > Because of limitations of my provider, I am forced to use the ftp
    > functions to upload files to my server (the permissions on the server
    > will not allow me to load files via php).
    >
    > I have successfully ported the sample code from php.net to do everything
    > BUT upload the file. I can connect. I can log in. I can change
    > directories. But the file never uploads. As I note in the code, I have
    > tried using ftp_put with the local filename, and I have tried using
    > ftp_fput with the file in /tmp/php with no luck. Suggestions are welcome.
    >
    > David
    >
    >
    Code:
    > ---------------------
    > <?php
    > $tmp = $_FILES['loadfile']; // passed in correctly as shown on next line
    > echo "<pre>" . print_r($tmp) . "</pre>";
    >
    > $upload_form_file = $tmp['name']; // have tried using the tmp_name
    > element combined with ftp_fput
    > $dest_file = "bob.gif"; // just a name to try
    >
    > // set up a connection or die
    > $conn_id = ftp_connect($ftp_server) or die("Couldn't connect to
    > $ftp_server"); // works
    >
    > // try to login
    > if (@ftp_login($conn_id, $ftp_user, $ftp_pass)) {
    >    echo "<p>Connected as $ftp_user@$ftp_server</p>\n"; // this is printed
    > } else {
    >    echo "Couldn't connect as $ftp_user\n";
    > }
    >
    > echo "<p>Current directory is now: " . ftp_pwd($conn_id) . "</p>\n"; //
    > this gives the root
    >
    > if (ftp_chdir($conn_id, $base_path)) {
    >    echo "<p>Current directory is now: " . ftp_pwd($conn_id) . "</p>\n";
    > // this prints /the/path/to/docs
    > } else {
    >    echo "Couldn't change directory\n";
    > }
    >
    > echo "<p>upload_form_file: $upload_form_file <br>\ndest_file: $dest_file
    > </p>\n"; // files are listed
    >
    > if (ftp_put($conn_id, $dest_file, $upload_form_file, FTP_BINARY)) {
    >  echo "successfully uploaded";
    > } else {
    >  echo "There was a problem while uploading $file\n"; // this always
    > triggers
    > }
    >
    > // close the connection
    > ftp_close($conn_id);
    >
    > ?>
    >
    [/color]

    Comment

    • Shawn Wilson

      #3
      Re: File Upload using ftp commands



      David wrote:[color=blue]
      >
      > Because of limitations of my provider, I am forced to use the ftp
      > functions to upload files to my server (the permissions on the server
      > will not allow me to load files via php).
      >
      > I have successfully ported the sample code from php.net to do everything
      > BUT upload the file. I can connect. I can log in. I can change
      > directories. But the file never uploads. As I note in the code, I have
      > tried using ftp_put with the local filename, and I have tried using
      > ftp_fput with the file in /tmp/php with no luck. Suggestions are welcome.
      >
      > David
      >
      >
      Code:
      > ---------------------
      > <?php
      > $tmp = $_FILES['loadfile']; // passed in correctly as shown on next line
      > echo "<pre>" . print_r($tmp) . "</pre>";
      >
      > $upload_form_file = $tmp['name']; // have tried using the tmp_name
      > element combined with ftp_fput
      > $dest_file = "bob.gif"; // just a name to try
      >
      > // set up a connection or die
      > $conn_id = ftp_connect($ftp_server) or die("Couldn't connect to
      > $ftp_server"); // works
      >
      > // try to login
      > if (@ftp_login($conn_id, $ftp_user, $ftp_pass)) {
      >     echo "<p>Connected as $ftp_user@$ftp_server</p>\n"; // this is printed
      > } else {
      >     echo "Couldn't connect as $ftp_user\n";
      > }
      >
      > echo "<p>Current directory is now: " . ftp_pwd($conn_id) . "</p>\n"; //
      > this gives the root
      >
      > if (ftp_chdir($conn_id, $base_path)) {
      >     echo "<p>Current directory is now: " . ftp_pwd($conn_id) . "</p>\n";
      > // this prints /the/path/to/docs
      > } else {
      >     echo "Couldn't change directory\n";
      > }
      >
      > echo "<p>upload_form_file: $upload_form_file <br>\ndest_file: $dest_file
      > </p>\n"; // files are listed
      >
      > if (ftp_put($conn_id, $dest_file, $upload_form_file, FTP_BINARY)) {
      >   echo "successfully uploaded";
      > } else {
      >   echo "There was a problem while uploading $file\n"; // this always
      > triggers
      > }
      >
      > // close the connection
      > ftp_close($conn_id);
      >
      > ?>
      >
      [/color]

      You will definitely have to use tmp_name.

      Are you sure the user you're connecting with has the ability to upload files to
      the specified directory? If you're not absolutely sure, try it manually with
      your ftp program.

      Also, you might want to put error_reporting (E_ALL); at the top of your script.

      Regards,
      Shawn
      --
      Shawn Wilson
      shawn@glassgian t.com

      Comment

      • Shawn Wilson

        #4
        Re: File Upload using ftp commands



        David wrote:[color=blue]
        >
        > Because of limitations of my provider, I am forced to use the ftp
        > functions to upload files to my server (the permissions on the server
        > will not allow me to load files via php).
        >
        > I have successfully ported the sample code from php.net to do everything
        > BUT upload the file. I can connect. I can log in. I can change
        > directories. But the file never uploads. As I note in the code, I have
        > tried using ftp_put with the local filename, and I have tried using
        > ftp_fput with the file in /tmp/php with no luck. Suggestions are welcome.
        >
        > David
        >
        >
        Code:
        > ---------------------
        > <?php
        > $tmp = $_FILES['loadfile']; // passed in correctly as shown on next line
        > echo "<pre>" . print_r($tmp) . "</pre>";
        >
        > $upload_form_file = $tmp['name']; // have tried using the tmp_name
        > element combined with ftp_fput
        > $dest_file = "bob.gif"; // just a name to try
        >
        > // set up a connection or die
        > $conn_id = ftp_connect($ftp_server) or die("Couldn't connect to
        > $ftp_server"); // works
        >
        > // try to login
        > if (@ftp_login($conn_id, $ftp_user, $ftp_pass)) {
        >     echo "<p>Connected as $ftp_user@$ftp_server</p>\n"; // this is printed
        > } else {
        >     echo "Couldn't connect as $ftp_user\n";
        > }
        >
        > echo "<p>Current directory is now: " . ftp_pwd($conn_id) . "</p>\n"; //
        > this gives the root
        >
        > if (ftp_chdir($conn_id, $base_path)) {
        >     echo "<p>Current directory is now: " . ftp_pwd($conn_id) . "</p>\n";
        > // this prints /the/path/to/docs
        > } else {
        >     echo "Couldn't change directory\n";
        > }
        >
        > echo "<p>upload_form_file: $upload_form_file <br>\ndest_file: $dest_file
        > </p>\n"; // files are listed
        >
        > if (ftp_put($conn_id, $dest_file, $upload_form_file, FTP_BINARY)) {
        >   echo "successfully uploaded";
        > } else {
        >   echo "There was a problem while uploading $file\n"; // this always
        > triggers
        > }
        >
        > // close the connection
        > ftp_close($conn_id);
        >
        > ?>
        >
        [/color]

        You will definitely have to use tmp_name.

        Are you sure the user you're connecting with has the ability to upload files to
        the specified directory? If you're not absolutely sure, try it manually with
        your ftp program.

        Also, you might want to put error_reporting (E_ALL); at the top of your script.

        Regards,
        Shawn
        --
        Shawn Wilson
        shawn@glassgian t.com

        Comment

        Working...