Building an FTP client with PHP

Articles

a quick look at FTP commands

mysqli - Improved mysql interface.

The case of the missing errors.

Putting together a web based File Transfer Program with PHP

You have probably seen that PHP has it's own in-built set of FTP functions. While it is a really good API if you are on a shared hosting enviorenment you might not be able to use it. Besides it's quite possible to create an FTP client from scratch without too much effort. This article is about creating a PHP FTP client using plain old sockets and FTP commands.

If you have been around this site you will know by now that our approach as usual will be to start of by the simplest bit of code and refine and enhance it in stages. At the end of this article we will try to show you a demo of the finished product.

The first step in creating our client will to open a connection to the server and switch to non blocking mode.

	
	$sock = fsockopen("localhost",21,&$errno,&$errstr,30);
	set_socket_blocking($sock,false);
	if($sock)
	{
		if(is_ok())
		{
			sock_write("user imapuser");

			if(is_ok())
			{
				sock_write("pass 123");
				is_ok();
			}
		}
	}
	

Blocking mode means if you attempt to read from the socket when data is not available the program will wait until it does become available. It's preferable at times to use blocking mode with multi threaded applications. However PHP isn't the ideal language for bulding them so we will stick to non blocking mode. In this mode,e if data is not available the function will just return without waiting, hence the term non blocking.

You can download the code on this page and try it out yourself. The server , username and password will need to be changed to suite your server. We will make convert it to class and make other changes in the next step


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