FTP directory listing with PHP Part II

New Articles

Is FTP secure?

PHP File upload progres bar.

The case of the missing errors.

LIST :: The directory listing command in the File Transfer Protocol.

We are about to see how to produce a formatted listing of the contents of directory on a FTP server. (Try reading that aloud on a single breath). with PHP. You can see it in action with the demo towards the end of this page.

The code below takes the response from the server splits it up into components and produces a formatted directory listing. Since this functionality is essentially part of the presentation we do not add it to our FTP class but keep it as a separate function. You can see it's listing below.

function show_list($data)


	$list = split("\n",$data);

	$pattern = "/[dwrx\-]{10}/";

	$list = array_slice($list,1,count($list)-1);

	foreach($list as $file)


		$file = preg_split("/ /",$file,20,PREG_SPLIT_NO_EMPTY);

		$downlink = "getfile.php?filename=". urlencode("$path/".$file[FNAME]);

		printf('<tr><td class="cell1">%s</td><td class="cell1">%s</td>

					<td class="cell1">%s</td><td class="cell1">%s</td>

					<td class="cell1">%s %s %s</td>

					<td  class="cell1"><a href="%s">%s</a></td>


				$file[FPERM],$file[FUID],$file[FGID], $file[FSIZE],


				$downlink, $file[FNAME]);



Ready for the demo? enter an FTP server's hostname or IP address followed by your username and password. We will then connect it to the server and try to retrieve a listing. If you do not have an FTP server that you can connect to, try a public FTP server that allows annonymous logins.

host : ftp://

SourceForge.net Logo

FTP Applet

  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