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.
Let's start at the top. Before you can get around to copying and moving files, you first need to be able to see them. And that's where PHP's directory commands come in - they provide you with an easy way to view and manipulate the contents of a specific directory on the system.
In order to access the contents of a directory, you first need to open a "handle" to the directory with the opendir() function; you can then read and manipulate the contents with readdir(). Once you're done, it's good programming practice to close the handle you've just opened with closedir().
The following example demonstrates these three steps:
<?
// get directory handle
$hook = opendir("/tmp");
// read directory and echo list
while (readdir($hook))
{
$file = readdir($hook);
echo "$file<br>";
}
// close directory
closedir($hook);
?>
A more professional approach is to write it the way the
manual recommends.
<?
// get directory handle
$hook = opendir("/tmp");
// read directory and echo list
while (($file = readdir($hook))!==false)
{
echo "$file<br>";
}
// close directory
closedir($hook);
?>
This article copyright Melonfire 2000. All rights reserved.{mospagebreak title=The Object-Oriented Approach} Another approach to obtain a directory listing is to use the PHP "dir" class, which creates a directory object with its own properties and functions. Take a look:
<?
// get directory handle
$hook = dir("/tmp");
// display location
echo "<b>Current path is $hook->path</b><br>";
// read directory and echo list
while ($file=$hook->read())
{
echo "$file<br>";
}
// close directory
$hook->close();
?>
In this case, three functions are available - read(), close()
and rewind() - together with two properties - $path, which records the current location in the filesystem, and $handle, which stores the resource ID for the directory handle.
And if you take a close look at the output from the two examples above, you'll see that PHP typically also displays the parent and current directories in the listing (look for the . and .. symbols.) As you'll see when I get to the composite example, it's much neater to filter these two items out of the directory listing.
<?
// get directory handle
$hook = dir("/tmp");
// display location
echo "<b>Current path is $hook->path</b><br>";
// read directory and echo list
while ($file=$hook->read())
{
if ($file != "." && $file != "..")
{
echo "$file<br>";
}
}
// close directory
$hook->close();
?>
And the output is:
index.html
.XF86Setup686
something.doc
file.jpg
Much neater!
Finally, there's the chdir() function,
which simply changes to a specific directory. You can combine this with what you've just seen to create a very simple file browser - take a look:
<html>
<head>
<basefont face="Arial">
</head>
<body>
<?
// if form has not been submitted, display form
if (!$submit)
{
?>
<center>
<form action=<? echo $PHP_SELF; ?> method=post>
Enter location: <input type=text name=location size=15>
<input type=submit name=submit value="Go">
</form>
</center>
<?
}
else
{
// change to directory
$res = chdir($location);
// if chdir() successful
if ($res)
{
// get directory handle
$hook = dir($location);
// display location
echo "<b>Current path is $hook->path</b><br>";
// read directory and echo list
while ($file=$hook->read())
{
if ($file != "." && $file != "..")
{
echo "$file<br>";
}
}
// close directory
$hook->close();
}
else
{
// display error
echo "<b>Unable to locate directory $location</b><br>";
}
}
?>
</body>
</html>
This article copyright Melonfire 2000. All rights reserved.{mospagebreak title=The Truth About Files And Directories} Now, let's move to files. As you've seen, the functions above simply display a combined listing of files and sub-directories in the selected directory. How about separating the two?
Well, PHP comes with a bunch of functions which let you distinguish between files and directories, and even between different types of files. The following snippet uses the is_dir() function to display directories in a different colour:
...
<?
// get directory handle
$hook = opendir($location);
// read directory and echo list
while (($file = readdir($hook)) !== false)
{
if ($file != "." && $file != "..")
{
// set up full path
$path = $location . "/" . $file;
// check for directory
if (is_dir($path))
{
echo "<font color=Red>$file</font><br>";
}
else
{
echo "<font color=Green>$file</font><br>";
}
}
}
// close directory
closedir($hook);
?>
...
In this case, the is_dir() function is used to identify
whether or not a directory is being listed. PHP also offers the is_file() and is_link() function to identify files and links respectively.
You can also obtain information on the listed files - size, permissions, ownership and the like. For example, the following code snippet shows you how to use the is_readable(), is_writeable() and is_executable() functions to display file properties, and the filesize() function to obtain the size of the file in bytes.
...
<?
// get directory handle
$hook = opendir($location);
// read directory and echo list
while (($file = readdir($hook)) !== false)
{
if ($file != "." && $file != "..")
{
// set up full path
$path = $location . "/" . $file;
echo "<table border=1 cellspacing=2>";
// set up attribute list
$att = "";
if (is_readable($path)) { $att .= "r"; }
if (is_writeable($path)) { $att .= "w"; }
if (is_executable($path)) { $att .= "x"; }
// check for directory
if (is_dir($path))
{
// print file information
echo "<tr><td width=200><font color=red>$file</font></td><td
width=100>0 bytes</td><td width=100>$att</td></tr>";
}
else
{
// get file size
$size = filesize($path);
// print file information
echo "<tr><td width=200>$file</td><td width=100>$size bytes</td><td
width=100>$att</td></tr>";
}
}
}
// close directory
closedir($hook);
echo "</table>";
?>
...
This article copyright Melonfire 2000. All rights reserved.