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

Permission Granted - 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.

  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



In addition to obtaining basic information on a file, PHP also comes with a set of functions designed to provide information on file sizes, permissions and modification times. Here's a list:

filesize() - gets size of file

filemtime() - gets last modification time of file

filemtime() - gets last access time of file

fileowner() - gets file owner

filegroup() - gets file group

fileperms() - gets file permissions

filetype() - gets file type

Consider the following example, which asks for a file name and displays the above information for that file:



// 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">
// else process form input
echo "Filename: <b>" . $_POST['file'] . "</b><br>";

// check if file exists
if (file_exists($_POST['file']))
// print file size
echo "File size: <b>" . filesize($_POST['file']) . " bytes</b><br>";

// print file owner
echo "File owner: <b>" . fileowner($_POST['file']) . "</b><br>";

// print file group
echo "File group: <b>" . filegroup($_POST['file']) . "</b><br>";

// print file permissions
echo "File permissions: <b>" . fileperms($_POST['file']) . "</b><br>";

// print file type
echo "File type: <b>" . filetype($_POST['file']) . "</b><br>";

// print file last access time
echo "File last accessed on: <b>" . date("Y-m-d",
fileatime($_POST['file'])) . "</b><br>";

// print file last modification time
echo "File last modified on: <b>" . date("Y-m-d",
filemtime($_POST['file'])) . "</b><br>";

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



Here's the output:

Filename: /apps/inventory_control/reports.php
File size: 1503 bytes
File owner: 512
File group: 100
File permissions: 33188
File type: file
File last accessed on: 2003-07-31
File last modified on: 2003-07-25

Note that the various file functions discussed in this section only return valid values if the underlying operating system supports them. On Windows, for example, it's quite likely that the fileowner(), filegroup() and
fileperms() functions will return null values.

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


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