File Upload

New Articles

On Jabber.

PHP File upload progres bar.

The case of the missing errors.

STOR command and FTP file upload with PHP

Earlier we looked at how files on a server can be downloaded using our PHP FTP client. That's only half the job done. Uploading to the server is just as important as downloading from it. So we begin the fourth part of this article by looking at how we can add the upload functionality to our File Transfer Program.

If you read part II of this article you know that with web based clients such as the one that we are building there isn't a direct connection between the user's computer and the server. Instead our web server act as a kind of proxy which means the files first need to be uploaded to the webserver over HTTP before they can be placed on the FTP server. I hope that you are familiar with upload handling with PHP, if not this might be a good time to try out the sample script included in the PHP manual.

Let's get down to business. The STOR command is used to transfer files to the server and it needs both a control connection and a data connection.

  
 	function stor($localPath,$remotePath)
	{
		if($this->pasv())
		{
			$this->sock_write("STOR $remotePath");
			if($this->is_ok())
			{
				$fp = fopen($localPath,"rb");
				while(!feof($fp))
				{
					$s = fread($fp,4096);
					fwrite($this->data_sock,$s);
				}
			}
		}
	}
  

As you see the code isn't lengthy but that's because we have not paid a lot of attention to error checking (yet) and the code to handle the HTTP upload isn't shown.

The $localPath variable above will usually contain the temporary name asigned by the PHP engine to the uploaded file while the $remotePath variable will be the 'original' name of the file on the user's computer.

If you are interested in adding an upload progress monitor please take a moment to check out mega upload. Megaupload can be easily integrated with the client that we are building, it's so easy in fact all you need to is to add two lines of code.
The next page we will look at how to implement the rename functionality.



SourceForge.net Logo

FTP Applet

Navigation
  Part 1   Introduction ,   Data Connection ,   Directory List ,   demo
  Part 2   Architecture ,   Downloading ,   Demo ,   Sessions
  Part 3   CWD ,   CDUP ,   Where am I? ,   Prune and Graft
  Part 4   Upload ,   Rename ,   Delete,   No operation