I want to allow visitors from my website to upload files to my server though a form on my site... I have all the ftp connection functions working, but when I get to the uploading (ftp_put) function it doesnt work.

$file = $HTTP_POST_VARS['file'];

if (ftp_put($conn_id, $file, $file, FTP_BINARY)) {
   echo "Successfully uploaded $file<br>";
} else {
   echo "There was a problem while uploading $file<br>";
}

What am I doing wrong?

    i dont understand how it works... if php is server side how is it suppost to read the file from the client to the server?

      It can read the file since the file has been sent to the server.

      You would need to use a form with enctype="multipart/form-data" and a type="file" input control, and then access the file with the $FILES array, e.g. $FILES['filename']

        Thanks for the reply, I tried what you said... but I can't get it to work.

        Here's my code:

        $ftp_server = '127.0.0.1';
        $ftp_user_name = "user";
        $ftp_user_pass = "pass";
        $conn_id = ftp_connect($ftp_server);
        
        // login with username and password
        $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
        
        // check connection
        if ((!$conn_id) || (!$login_result)) {
               echo "FTP connection has failed!";
               echo "Attempted to connect to $ftp_server for user $ftp_user_name";
               exit;
           } else {
               echo "Connected to $ftp_server, for user $ftp_user_name";
           }  
        
        // upload the file
        if (ftp_put($conn_id, $_FILES['userfile']['name'], $_FILES['userfile']['name'], FTP_BINARY)) {
           echo "Successfully uploaded $file<br>";
        } else {
           echo "There was a problem while uploading $file<br>";
        }
        // close the FTP stream
        ftp_close($conn_id);

        When I run this I think it gets to the "ftp_put" functions and fail. How can I fix this?

          Do you know the structure of the $_FILES array?

          You should use
          ftp_put($conn_id, $FILES['userfile']['name'], $FILES['userfile']['tmp_name'], FTP_BINARY)

            I am still having problems with uploading... I have read almost all of the manual, tested it, and it continues not to work. Could something be wrong with my php setup or webserver?

              How does it fail (i.e. what errors do you get)?

              What is the HTML form like?

              Can you print the current working directory?
              Have you tried just navigating directories using FTP with PHP?
              What about deleting a test file?

                Ok....I got it to upload files. Now when I upload it sends to the root folder of my ftp server. I added this [below] to try to move it to a differnt directory.

                //after upload
                $uploaddir = '/www/uploads/';
                $uploadfile = $uploaddir . $_FILES['userfile']['name'];
                
                print "<pre>";
                if (move_uploaded_file($_FILES['userfile']['name'], $uploadfile)) {
                   print "File is valid, and was successfully uploaded. ";
                   print "Here's some more debugging info:\n";
                   print_r($_FILES);
                } else {
                   print "Possible file upload attack!  Here's some debugging info:\n";
                   print_r($_FILES);
                }
                print "</pre>"; 

                but It doesn't work.

                  nvm... i got it working...

                  ftp_chdir($conn_id, "www/uploads/")

                  I changed the directory. Thanks for your help.

                    Write a Reply...