HomePHP Page 6 - File And Directory Manipulation In PHP (part 1)
A Matter Of Existence - PHP
PHP comes with a powerful and flexible file manipulation API that allows developers (among other things) to read and write files, view and modify file attributes, read and list directory contents, alter file permissions, and retrieve file contents into a variety of native data structures. Find out more, inside.
PHP also comes with a bunch of functions that allow you to test the status of a file - for example, find out whether it exists, whether it's empty, whether it's readable or writable, and whether it's a binary or text file. Of these, the most commonly used operator is the file_exists() function, which is used to test for the existence of a specific file.
Here's an example which asks the user to enter the path to a file in a Web form, and then returns a message displaying whether or not the file exists:
<html>
<head>
</head>
<body>
<?php
// if form has not yet been submitted
// display input box
if (!$_POST['file'])
{
?>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
Enter file path <input type="text" name="file">
</form>
<?
}
// else process form input
else
{
// check if file exists
// display appropriate message
if (file_exists($_POST['file']))
{
echo "File exists!";
}
else
{
echo "File does not exist!";
}
}
?>
</body>
</html>
There are many more such functions - here's a brief list, together with an example that builds on the one above to provide more information on the file specified by the user.
is_dir() - returns a Boolean indicating whether the specified path is a directory
is_file() - returns a Boolean indicating whether the specified file is a regular file
is_link() - returns a Boolean indicating whether the specified file is a symbolic link
is_executable() - returns a Boolean indicating whether the specified file is executable
is_readable() - returns a Boolean indicating whether the specified file is readable
is_writable() - returns a Boolean indicating whether the specified file is writable
And here's a script that demonstrates how you could use these operators to obtain information on any file on your system:
<html>
<head>
</head>
<body>
<?php
// if form has not yet been submitted
// display input box
if (!$_POST['file'])
{
?>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
Enter file path <input type="text" name="file">
</form>
<?
}
// else process form input
else
{
$output = "Filename: <b>" . $_POST['file'] . "</b><br>";
// check if file exists
// display appropriate message
if (file_exists($_POST['file']))
{
// is it a directory?
if (is_dir($_POST['file']))
{
$output .= "File is a directory <br>";
}
// is it a file?
if (is_file($_POST['file']))
{
$output .= "File is a regular file <br>";
}
// is it a link?
if (is_link($_POST['file']))
{
$output .= "File is a symbolic link <br>";
}
// is it executable?
if (is_executable($_POST['file']))
{
$output .= "File is executable <br>";
}
// is it readable?
if (is_readable($_POST['file']))
{
$output .= "File is readable <br>";
}
// is it writable?
if (is_writable($_POST['file']))
{
$output .= "File is writable <br>";
}
}
else
{
$output .= "File does not exist! <br>";
}
echo $output;
}
?>
</body>
</html>
And here's what the output might look like:
Filename: /home/me/addresses.dat File is a regular file File is executable File is readable File is writable