Home arrow PHP arrow Page 8 - File And Directory Manipulation In PHP (part 1)

In Stat We Trust - 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

An alternative way of obtaining file information is via the stat() function, which retrieves detailed status information on the named file. The return value from stat() is an array consisting of the following elements (take a look at the PHP manual page for the stat() function for more information on what each array element represents):


ARRAY WHAT IT
KEY MEANS
------------------------------
dev device number
ino inode number
mode inode protection mode
nlink number of links
uid file owner's user ID
gid file owner's group ID
rdev device type
size file size (in bytes)
atime file's last access timestamp
mtime file's last modify timestamp
ctime file's last change timestamp
blksize filesystem block size
blocks file's allocated blocks

Consider the following example, and its output, which illustrate how stat()
works:


<?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
{
echo "Filename: <b>" . $_POST['file'] . "</b><br>";

// check if file exists
if (file_exists($_POST['file']))
{
// get file status
$stats = stat($_POST['file']);

// iterate over array and print information
foreach ($stats as $k=>$v)
{
if (!is_numeric($k))
{
echo "$k: <b>$v</b><br>";
}
}

}
else
{

echo "File does not exist! <br>";

}
}
?>

</body>
</html>

Here's an example of the output:


Filename: /home/web/apps/library/index.php
dev: 2049
ino: 767302
mode: 33188
nlink: 1
uid: 512
gid: 100
rdev: 21884
size: 1503
atime: 1059626194
mtime: 1059118092
ctime: 1059118095
blksize: 4096
blocks: 8

Note that stat() will return -1 for those attributes which are unsupported by the underlying operating system.



 
 
>>> 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: