Page 1 of 2

Problem using FTP functions

Posted: Thu Oct 14, 2010 3:54 pm
by snowc
I am in the final stage of a site development. I created a website where doctors can register and share their medical case. To do that I had to support video and photo uploads.

The photo upload process is done and functions perfectly using HTTP protocol.

Now the nasty part, I did use PHP's ftp functions before and I believe that most of the example on the web are no good.

Most of the examples of the web use a form that uploads the file and use ftp_put to upload the file!!!! If they used the form does not that mean that they uploaded the file using the HTTP protocol? What's the point of using FTP if you're going to upload the file using HTTP then move it using the FTP protocol functionality? I would really appreciate if some one break down how ftp functions work and not using the HTTP protocol.

An example of what I think is bad way of using the ftp functions

Code: Select all

<?php
  //-- SMTP Mail Function  By Aditya Bhatt
  if(isset($_POST['SubmitFile'])){
      $myFile = $_FILES['txt_file']; // This will make an array out of the file information that was stored.
      $file = $myFile['tmp_name'];  //Converts the array into a new string containing the path name on the server where your file is.
 
      $myFileName = basename($_FILES['txt_file']['name']); //Retrieve filename out of file path

      $destination_file = $_REQUEST['filepath'].$myFileName;
      #"/developers/uploadftp/aditya/".$myFileName;  //where you want to throw the file on the webserver (relative to your login dir)

      // connection settings
      $ftp_server = trim($_REQUEST['serverip']);  //address of ftp server.
      $ftp_user_name = trim($_REQUEST['username']); // Username
      $ftp_user_pass = trim($_REQUEST['password']);   // Password

      $conn_id = ftp_connect($ftp_server) or die("<span style='color:#FF0000'><h2>Couldn't connect to $ftp_server</h2></span>");        // set up basic connection
      #print_r($conn_id);
      $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass) or die("<span style='color:#FF0000'><h2>You do not have access to this ftp server!</h2></span>");   // login with username and password, or give invalid user message
      if ((!$conn_id) || (!$login_result)) {  // check connection
             // wont ever hit this, b/c of the die call on ftp_login
             echo "<span style='color:#FF0000'><h2>FTP connection has failed! <br />";
             echo "Attempted to connect to $ftp_server for user $ftp_user_name</h2></span>";
             exit;
         } else {
         //    echo "Connected to $ftp_server, for user $ftp_user_name <br />";
      }

      $upload = ftp_put($conn_id, $destination_file, $file, FTP_BINARY);  // upload the file
      if (!$upload) {  // check upload status
         echo "<span style='color:#FF0000'><h2>FTP upload of $myFileName has failed!</h2></span> <br />";
      } else {
         echo "<span style='color:#339900'><h2>Uploading $myFileName Completed Successfully!</h2></span><br /><br />";
      }

      ftp_close($conn_id); // close the FTP stream
  }
?>

<html>
  <head></head>
  <body>
        <form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
            Server IP Address: <input name="serverip" type="text" id="serverip" size="15" value=""/><br>
            Server Username: <input name="username" type="text" id="username" size="15" value=""/><br>
            Server Password: <input name="password" type="text" id="password" size="15" value=""/><br>
            Server File Path: <input name="filepath" type="text" id="filepath" size="35" value=""/><br>
            Please choose a file: <input name="txt_file" type="file" id="txt_file" tabindex="1" size="35" onChange="txt_fileName.value=txt_file.value" /><br><br>
            <input name="txt_fileName" type="hidden" id="txt_fileName" tabindex="99" size="1" />

            <input type="submit" name="SubmitFile" value="Upload File" accesskey="ENTER" tabindex="2" />
      </form>
  </body>
</html>

Re: Problem using FTP functions

Posted: Thu Oct 14, 2010 4:18 pm
by John Cartwright
Ftp class functions are generally only used when you want to upload something to an external ftp server. If the client has already uploaded a file, there is no reason to use FTP functions unless, as I mentioned, you want to move the file to another server. Otherwise, you are essentially uploading the file twice, for no real purpose.

Once the user has uploaded a file to your tmp dir, you would simply need to use move_uploaded_file() to move the file to a permanent destination on your server.

Re: Problem using FTP functions

Posted: Fri Oct 15, 2010 6:14 am
by snowc
John Cartwright wrote:Ftp class functions are generally only used when you want to upload something to an external ftp server. If the client has already uploaded a file, there is no reason to use FTP functions unless, as I mentioned, you want to move the file to another server. Otherwise, you are essentially uploading the file twice, for no real purpose.

Once the user has uploaded a file to your tmp dir, you would simply need to use move_uploaded_file() to move the file to a permanent destination on your server.
That's why I am asking for help!!! The examples on the php manual uploads the files twice, I noticed that. The problem is how to upload the file using ftp protocol (that means I should not use a form or at least not submit the data as POST)??

I successfully connected and logged to the server and I can't figure out how the ftp_put function works. All of the examples on the web use the HTTP protocol to upload the file and move it with ftp_put (of course it does not work since the ftp put won't find the temp path). And the HTTP protocol is useless when uploading big files which is my goal.

Could any one explain to me how
1- to use the ftp_put function.
2- How to get the file path on the local drive in order to upload the file using ftp_put. (Form does not seem to do the trick because it does not submit the file path)

I already know, in the example, that the file was uploaded using HTTP protocol than the temp name was passed to the ftp function. I believe that will generate an error and the code won't work because what's being done is moving the file from the server to the server it self.

Thanks,

Re: Problem using FTP functions

Posted: Fri Oct 15, 2010 6:51 am
by VladSun
Why do you want to use FTP at all. As John Cartwright, it would make sense *only* if you needed to put files on another server (machine). Do you have two servers?

Re: Problem using FTP functions

Posted: Sat Oct 16, 2010 1:28 pm
by snowc
VladSun wrote:Why do you want to use FTP at all. As John Cartwright, it would make sense *only* if you needed to put files on another server (machine). Do you have two servers?
I am building medical website where doctors can write about their medical cases and upload case videos. I since the HTTP protocol is not suitable for big file transfer I want to use the FTP protocol instead to make the transfer.

Re: Problem using FTP functions

Posted: Sat Oct 16, 2010 1:47 pm
by VladSun
So you will definitely need an FTP client installed on all doctor's PCs and an FTP server. I can't see how PHP will be in help though (unless you want to build your own desktop based (i.e. not web based) FTP client in PHP).

http://daniel.haxx.se/docs/ftp-vs-http.html

Re: Problem using FTP functions

Posted: Sat Oct 16, 2010 4:23 pm
by josh
What you want to do is setup one FTP server on the same machine as the web server where PHP runs. Have each user name their file a specific filename, and in your form have a standard text input. Let them enter in the name of the file they uploaded.

Re: Problem using FTP functions

Posted: Sun Oct 17, 2010 3:23 am
by VladSun
josh wrote:What you want to do is setup one FTP server on the same machine as the web server where PHP runs. Have each user name their file a specific filename, and in your form have a standard text input. Let them enter in the name of the file they uploaded.
And how would they perform the FTP upload itself?

Re: Problem using FTP functions

Posted: Mon Oct 18, 2010 3:09 am
by snowc
I was thinking about it and the more I think about it the more it seems nonsense.

Code: Select all

bool ftp_put  ( resource $ftp_stream  , string $remote_file  , string $local_file  , int $mode  [, int $startpos = 0  ] )
I thought that the ftp_put works as follows
1- We connect and log to the server where the site is located.
2- The user passes a file path. Note that the file is located on their local machine.
3- The PHP function transfers the file to server.

At least that's how the examples on PHP manual where doing it. Except it does not work because they use HTTP protocol to upload the file and then pass the temp name as the remote file.

I guess that people write all sort of crap (bunch of w*n*ers).

I don't think that PHP can browse or modify the client's machine hence the function won't work the way it's indicated on PHP manual.

I think that I will try to create an app using PHP and Flex to upload the files. I don't think that I can ask the doctors (god knows it's enough they know how to use the mouse) to set up a ftp server on their machines to upload videos.

Thank you guys for trying to help

This is function manual of PHP.ORG (http://php.net/manual/en/function.ftp-put.php). Check the link for the examples where users use HTTP and FTP to upload files. PHP.ORG should verify the example posted on their site so they don't wast everyone's time.

Re: Problem using FTP functions

Posted: Mon Oct 18, 2010 9:45 am
by josh
VladSun wrote:And how would they perform the FTP upload itself?
FileZilla, or any other desktop FTP client.

Re: Problem using FTP functions

Posted: Mon Oct 18, 2010 12:13 pm
by VladSun
josh wrote:
VladSun wrote:And how would they perform the FTP upload itself?
FileZilla, or any other desktop FTP client.
So why do we need this :[quote="josh"Have each user name their file a specific filename, and in your form have a standard text input. Let them enter in the name of the file they uploaded.[/quote] ?

Re: Problem using FTP functions

Posted: Mon Oct 18, 2010 11:56 pm
by josh
@Vlad So they can bind the file to some database record & store information about it?
I am building medical website where doctors can write about their medical cases and upload case videos.

Re: Problem using FTP functions

Posted: Tue Oct 19, 2010 12:26 am
by Christopher
snowc wrote:I think that I will try to create an app using PHP and Flex to upload the files. I don't think that I can ask the doctors (god knows it's enough they know how to use the mouse) to set up a ftp server on their machines to upload videos.

Thank you guys for trying to help
You have not seemed to listen to a word they have written. And you don't seem to understand FTP or the PHP library for FTP.

Definitely use HTTP upload. I don't know where you got the idea that it is not good for large uploads. There are server settings you will need to change to allow large files and longer upload times. And you probably want to look into upload scripts that allows restart after failure. I would not recommend that you write it yourself.

Re: Problem using FTP functions

Posted: Tue Oct 19, 2010 5:18 am
by snowc
Is not that am not listening to others.

The site is used by several doctors who can upload videos. At the beginning I thought that the FTP functionality can upload files just like forms do. VladSun (thanks dude) mentioned ftp is used to transfer file from a server to another.

The medical association has 100 docs signed as members so FileZilla is out of the question.

I have tried the HTTP protocol but it has upload limits depending on the host (and yes I did modify the php.ini). I will defiantly check some of the scripts that allow upload resume.

I was thinking of creating an application using flex and php.

1- The user enters info about the file and gets a code. (Server side)
2- Enters the code in the app and the app checks the database for the destination path and uploads the file.

I think it's the best way to do it. I think that most of the major file hosts are using some kind of an application.

I would like to thank you all for your help.

Re: Problem using FTP functions

Posted: Tue Oct 19, 2010 5:22 am
by snowc
josh wrote:@Vlad So they can bind the file to some database record & store information about it?
I am building medical website where doctors can write about their medical cases and upload case videos.
I will post you a link tonight so you can see it in action. (France time). The doctors are supposed to write articles about cases and post photos and videos about the case they're writing about.

Thanks for trying help.