Home arrow PHP arrow 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.

TABLE OF CONTENTS:
  1. File And Directory Manipulation In PHP (part 1)
  2. Handle With Care
  3. Different Strokes
  4. Weapon Of Choice
  5. Weather Balloon
  6. A Matter Of Existence
  7. Permission Granted
  8. In Stat We Trust
  9. A Short Break
By: icarus, (c) Melonfire
Rating: starstarstarstarstar / 74
August 07, 2003

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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



 
 
>>> More PHP Articles          >>> More By icarus, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: