You've already seen how PHP4's FTP functions can help youinteract with files on a remote server. In this article, learn how to usePHP's other file and directory manipulation functions, and build anequivalent application that demonstrates the difference between the twoapproaches.
Now that you know the various techniques, it's time to build a simple file manager. You can download the complete source code from here - I'll just give you a quick tour through the various sections.
"index.php" sets up the main interface for the applications. On the left side is a list of available functions, while the right side contains a listing of files and directories. Directories can be entered by clicking on them, while files located within the Web server document root can be downloaded by clicking on them (files outside the server root cannot be downloaded). The script "index.php" can accept the parameter $dir, which indicates the directory to be displayed.
All this takes place via two functions, links() and filelist(), which you can see in the file "links.php" - the former sets up the links on the left side, while the latter uses the opendir() and other file information functions to obtain information on the size and permissions of files in the directory. Directories are set up as active hyperlinks - once a directory is clicked, the script "index.php" is called once again, but with a different $dir value.
At any point, you can access the functions on the left side - "upload.php" accepts a file for upload, "mkdir.php" allows you to create a new directory in the current locations, "rmdir.php" reverses the process, and "rmfile.php" allows you to delete one or more files. All these scripts accept a single parameter, $dir, which tells them where their respective actions are to be performed.
It's also possible to download files by turning them into clickable hyperlinks - the application checks to first make sure that the files are in the Web server DocumentRoot, sets a flag if they are, and then turns each file into a hyperlink. Again, there are security implications here - so be careful!
And once you're done, it might be worthwhile to go back to the equivalent file browser we built in the first part of this article. If you compare the two approaches, other things being equal, you'll notice that the HTTP file browser is much faster than the FTP file browser, and also allows more flexibility when manipulating files.
Hopefully, this exercise has helped you gain a clearer idea of PHP's file manipulation capabilities, together with an understanding of what is and what isn't possible. I'm off - but I'll see you soon!
This article copyright Melonfire 2000. All rights reserved.