Building An FTP Client With PHP - Building an FTP App
(Page 5 of 7 )
We've covered the basics of PHP and FTP so far. Let's now take what we've learnt and build a simple browser based FTP application that will allow us to retrieve files, send files, create remote directories, and both rename and delete remote files. The code for our FTP app is included as part of the support material for this article, so I will concentrate on explaining only the logic behind the code.
First off, all of our code will be contained in one file called ftp.php. It will contain several functions that will handle various tasks. A switch statement in combination with some checking of query string variables will be used to tell the script what to do next:
$command = "";
if(isset($_GET["command"]))
$command = $_GET["command"];
else
$command = "listFiles";
switch($command)
{
case "listFiles":
ShowFiles();
break;
case "getFile":
GetFile();
break;
case "putFile":
PutFile();
break;
case "renameFile":
RenameFile();
break;
case "deleteFile":
DeleteFile();
break;
default:
ShowFiles();
} By calling up ftp.php directly in our browser, the ShowFiles function is called. ShowFiles simply lists and files and directories contained within the current working directory. If no working directory is passed in as a query string variable then the default directory retrieved from ftp_pws is used.
The ShowFiles function ShowFiles is the heart of our FTP application. It generates a list of files from the remote server and shows the as either a directory or file link in our browser. If a directory is found, then it's displayed with a directory icon. If a file is found, then it's displayed with a file icon. When a directory link is clicked, its contents are displayed with a call to ShowFiles. When a file is clicked, it is saved to our local machine with a call to GetFile.
In our FTP app, when you click on a file to download it, it is prefixed with ___ (three underscores) followed by the file name and saved in your root directory. So, for example, if you click on readme.txt, then it will exist as /___readme.txt on your machine after it has been downloaded through our FTP script.
One function that I haven't mentioned is DoConn. DoConn connects to the FTP server of our choice and returns its connection resource if connected successfully:
function DoConn()
{
global $ftpServer;
global $ftpUser;
global $ftpPass;
$c = @ftp_connect($ftpServer);
$l = @ftp_login($c, $ftpUser, $ftpPass);
if(!$c)
die("A connection to $ftpServer couldn't be established");
else if(!$l)
die("Your login credentials were rejected");
else
return $c;
} DoConn is called in ShowFiles, just like this:
function ShowFiles()
{
$conn = DoConn();
$currDir = "";
// Get the name of the current directory
if(isset($_GET["currDir"]))
$currDir = $_GET["currDir"];
else
$currDir = ftp_pwd($conn); As shown above, ShowFiles checks the $_GET array (which holds all query string variables) to check if currDir (the current working directory) is specified. If not, ftp_pwd is used to retrieve the working directory from the FTP server.
Next, we retrieve the list of directories and files from the FTP server using ftp_nlist, as we’ve seen earlier:
// Retrieve a list of files and directories
// and display the appropriate icon for each
$fList = @ftp_nlist($conn, $currDir); To allow us to traverse directories on our FTP server through our web browser, we need to be able to move back up the directory chain. We use a simple call to ereg_replace to find the parent directory:
// We will work out the parent directory
$parentDir = strrev($currDir);
$parentDir = ereg_replace("^[a-zA-Z0-9\-]*/", "", $parentDir);
$parentDir = strrev($parentDir); We then display the parent directory (if any) as two dots, in the same way that many FTP programs do:
<img src='parent.gif'>
<a href='ftp.php?command=listFiles&currDir=<?php echo $parentDir; ?>'>
..
</a>
<br> Next, we loop through each index in the array generated by ftp_nlist and use a nifty trick to work out whether it's a file or directory: If we can change directories into it without an error occurring then it's a directory, if not it's a file:
for($i = 0; $i < sizeof($fList); $i++)
{
// We will remove the parent directory (if any)
// from the name of the file that gets displayed
$trimFile = ereg_replace("^$currDir", "", $fList[$i]);
// Remove any forward slash at front of name
$trimFile = ereg_replace("^/", "", $trimFile);
if(@ftp_chdir($conn, $fList[$i]))
{
@ftp_cdup($conn);
?>
<img src='folder.gif'>
<a href='ftp.php?command=listFiles&currDir=<?php echo $fList[$i]; ?>'>
<?php echo $trimFile; ?>
</a>
<br>
<?php
}
else
{
?>
<img src='file.gif'>
<a href='ftp.php?command=getFile&currFile=<?php echo $fList[$i]; ?>&currDir=<?php echo $currDir; ?>'>
<?php echo $trimFile; ?>
</a>
<br>
<?php
}
}
} When you download the support file, the FTP connect and login details are set to ftp.microsoft.com by default, just to demonstrate the script. After loading it in the browser, here's what it looks like initially:
Pretty neat 'ey? As we delve deeper and deeper into the directory tree, we can see that files are displayed with different icons, and also the path to the parent directory is displayed with the arrow icon, like so:
Each file is actually a link to ftp.php with some query string parameters so that the GetFile function is called. By clicking on the q184305.txt link in the example above, we end up at this URL:
ftp.php?command=getFile&currFile=/bussys/utilities/hotfix/q184305.txt&currDir=/bussys/utilities/hotfix
Breaking this up, we have three query string parameters:
- ftp.php?
- command=getFile
- &currFile=/bussys/utilities/hotfix/q184305.txt
- &currDir=/bussys/utilities/hotfix
The command parameter tells our script to run the GetFile function. CurrFile is the location of the file to be downloaded, and currDir is used to relist files in the directory where we were before we started getting the file.
Next: The GetFile function >>
More PHP Articles
More By Mitchell Harper