8 Replies Latest reply on Oct 1, 2007 7:28 PM by Newsgroup_User

    PHP ftp_put function

    MikeL7 Level 1
      I have made a page to allow users to upload files larger than 2M to a hosted server (Cant change upload_file_max_size) and have this proccessing script, it connects but error comes back with error "ftp upload has failed", cant figure out why?

      $conn_id = ftp_connect('ftp.mysite.com');
      // login with username and password
      $login_result = ftp_login($conn_id,'ftpuser@mysite.com', 'password');

      // check connection
      if ((!$conn_id) || (!$login_result)) {
      echo "ftp connection has failed!";
      exit;
      } else {
      echo "Connected to server!";
      }

      // upload the file
      $file = $_POST['file_upload'];

      $dir = "/public_html/folder/$file_name";
      $upload = ftp_put($conn_id, $dir, $file, FTP_BINARY);

      // check upload status
      if (!$upload) {
      echo "ftp upload has failed!";
      } else {
      echo "uploaded $file to mysite.com";
      }
        • 1. Re: PHP ftp_put function
          Level 7
          MikeL7 wrote:
          > I have made a page to allow users to upload files larger than 2M to a hosted
          > server (Cant change upload_file_max_size) and have this proccessing script, it
          > connects but error comes back with error "ftp upload has failed", cant figure
          > out why?

          The problem lies in the following line:

          > // upload the file
          > $file = $_POST['file_upload'];

          You need to pass the full path of the local file as the third argument
          to ftp_put(). This won't do it. Go to the following page in the PHP manual:

          http://www.php.net/manual/en/function.ftp-put.php

          Take a look at the first user-contributed note, which shows a JavaScript
          solution to populating a hidden field in the form with the full local
          path. I haven't tried it, but it might put you on the right track.

          --
          David Powers, Adobe Community Expert
          Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
          Author, "PHP Solutions" (friends of ED)
          http://foundationphp.com/
          • 2. Re: PHP ftp_put function
            MikeL7 Level 1
            I made the hidden field the file path from the file input form field, and now i get a error file doesnt exist?

            Connected to server!
            Warning: ftp_put(C:\Documents and Settings\Administrator\My Documents\My Music\Track No10.mp3) [function.ftp-put]: failed to open stream: No such file or directory in /public_html/photos/uploadmusic.php on line 84
            ftp upload has failed!

            I think the script is looking in the server and not the local machine for the file. Line 84 is $upload = ftp_put($conn_id, $dir, $file, FTP_BINARY);
            • 3. Re: PHP ftp_put function
              Level 7
              MikeL7 wrote:
              > I think the script is looking in the server and not the local machine for the
              > file. Line 84 is $upload = ftp_put($conn_id, $dir, $file, FTP_BINARY);

              I have never tried using the PHP FTP functions, so am just as much in
              the darks as you. However, consulting a couple of books that I have got,
              it looks as though the FTP function you're trying to use is designed for
              running on your local machine, not on a remote server.

              It looks as though you might need to use fopen() to read the local file
              into a resource variable, and then use ftp_fput() to upload it.

              http://www.php.net/manual/en/function.ftp-fput.php

              --
              David Powers, Adobe Community Expert
              Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
              Author, "PHP Solutions" (friends of ED)
              http://foundationphp.com/
              • 4. PHP ftp_put function
                MikeL7 Level 1
                I have the ftp_fput function working if i run the script on the local machine (just like ftp_put) it ftp uploads the file selected to the remote server (evan files larger than 2M), but when i put the script on the server and run it i get this error

                Warning: fopen(C:\Program Files\Apache Group\Apache2\htdocs\Photos\mp3\test23.php) [function.fopen]: failed to open stream: No such file or directory in /public_html/photos/uploadmusic2.php on line 78

                Warning: ftp_fput() expects parameter 3 to be resource, boolean given in /public_html/photos/uploadmusic2.php on line 83

                I thinnk i am back to the same problem the script is looking for the file to open, but not on the local machine. If i dont use the stripslashes then i get two slashes in the path ex. C:\\folder\\subfolder\\file_to_upload.txt in the error message

                This is what i have:

                $conn_id = ftp_connect('ftp.mysite.com');
                // login with username and password
                $login_result = ftp_login($conn_id,'ftpuser@mysite.com', 'password');

                // check connection
                if ((!$conn_id) || (!$login_result)) {
                echo "ftp connection has failed!";
                exit;
                }
                $file = stripslashes($_POST['file_path']);
                $fh = fopen($file, 'r');

                $track_name = ($_FILES[m_file][name]);
                $dir = "$track_name";
                $upload = ftp_fput($conn_id, $dir, $fh, FTP_BINARY);

                // close the ftp stream
                ftp_close($conn_id);

                • 5. Re: PHP ftp_put function
                  Level 7
                  MikeL7 wrote:
                  > I thinnk i am back to the same problem the script is looking for the file to
                  > open, but not on the local machine.

                  I've been thinking about it a bit more, and I can't see any way that a
                  PHP script can read a file from a client computer to upload it to an FTP
                  site. Just think of the security implications. It would give
                  unscrupulous people the ability to probe other people's computers and
                  steal their documents. The source file needs to be on the same computer.

                  --
                  David Powers, Adobe Community Expert
                  Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
                  Author, "PHP Solutions" (friends of ED)
                  http://foundationphp.com/
                  • 6. Re: PHP ftp_put function
                    MikeL7 Level 1
                    I think you are right the function is designed to upload files from a server to another server(testing, backup or production). I am trying to find a way around the 'upload_max_filesize' 2M limit in the php.ini file as my host wont allow changing.
                    Could i use the http form and then write the uploaded file to my ftp folder(on same server)?
                    As i understand, when you upload a file that is to large it still goes to the server, but the move_uploaded_file function is where the 2M limit is enforced?
                    If so then i could ftp the file from the temp directory to my ftp folder because the file is already on the server?

                    • 7. Re: PHP ftp_put function
                      Level 7
                      MikeL7 wrote:
                      > I think you are right the function is designed to upload files from a server to
                      > another server(testing, backup or production). I am trying to find a way around
                      > the 'upload_max_filesize' 2M limit in the php.ini file as my host wont allow
                      > changing.
                      > Could i use the http form and then write the uploaded file to my ftp folder(on
                      > same server)?
                      > As i understand, when you upload a file that is to large it still goes to the
                      > server, but the move_uploaded_file function is where the 2M limit is enforced?
                      > If so then i could ftp the file from the temp directory to my ftp folder
                      > because the file is already on the server?

                      Have you tried creating an .htaccess file and putting PHP configuration
                      commands in that? I've done this in the past and it's worked for me.
                      Just create an empty text file and put the following two lines in it:

                      php_value upload_max_filesize 64M
                      php_value post_max_size 64M

                      Then upload this to the root of the website. This example sets the
                      upload limit to 64 megabytes but you can change this by changing the
                      "64" in the example.
                      • 8. Re: PHP ftp_put function
                        Level 7
                        .oO(Murray Moffatt)

                        >Have you tried creating an .htaccess file and putting PHP configuration
                        >commands in that? I've done this in the past and it's worked for me.

                        It works if PHP is installed as an Apache module. For the CGI version
                        you would have to alter the php.ini or use your own php.ini if the host
                        allows that (some do).

                        Micha