Jump to: navigation, search

PHP File Information

From w3cyberlearnings

Contents

PHP File Information

  • Get file information is very important.
  • File Size, Access time, update time, and modify time
  • File access such file readable, executable, and writable.

List of functions

filectime(filename) ; // file change time
fileatime(filename) ; // file access time 
filemtime(filename) ; // file update time

file_exists(filename);   // file exists
is_executable(filename); // file is executable
is_readable(filename);   // file is readable

is_file(filename);       // check it is file
file_size(filename);     // get file size

Example 1

  • Make sure you have create a text file and name it as book.txt.
  • Note: When you run this progrma, you will a different result.
<?php

$my_file = 'book.txt';
getFileInfo($my_file);

function getFileInfo($filename) {
	if (!file_exists($filename)) {
		echo "$filename does not exist <br/>";
	}
	$exec_f = (is_executable($filename) ? "yes" : "no");
	$write_f = (is_writable($filename) ? "yes" : "no");
	$read_f = (is_readable($filename) ? "yes" : "no");
	$a_f = (is_file($filename) ? "yes" : "no");
	$d_f = (is_dir($filename) ? "yes" : "no");
	echo "$filename is .. <br/>";
	echo "Executable: " . $exec_f . "<br/>";
	echo "Writable: " . $write_f . "<br/>";
	echo "Readable:" . $read_f . "<br/>";
	echo "Is A file:" . $a_f . "<br/>";
	echo "Is A Directory:" . $d_f . "<br/>";
	echo "File Change time: " . date("D, Y M d g:i A", filectime($filename)) . "<br/>";
	echo "File Access time: " . date("D, Y M d g:i A", fileatime($filename)) . "<br/>";
	echo "File Update time: " . date("D, Y M d g:i A", filemtime($filename)) . "<br/>";
	echo "File Size: " . filesize($filename) . "bytes or " . (filesize($filename) / 1023) . "M<br/>";
	echo "File Permission: " . substr(base_convert(fileperms($filename), 10, 8), 3);
}
?>

Output

book.txt is .. 
Executable: no
Writable: yes
Readable:yes
Is A file:yes
Is A Directory:no
File Change time: Tue, 2012 May 15 5:27 PM
File Access time: Tue, 2012 May 15 5:27 PM
File Update time: Tue, 2012 May 15 5:27 PM
File Size: 8bytes or 0.0078201368523949M
File Permission: 666

Related Links


  1. PHP File Linux vs. Windows
  2. PHP File Information
  3. PHP File Create
  4. PHP File Close
  5. PHP File Read
  6. PHP File Read Reverse
  1. PHP File Write
  2. PHP File Write to end or append to
  3. PHP File Write at Beginning
  4. PHP File Write at specific location
  5. PHP File Truncate
  6. PHP File Replace a specific word
  1. PHP File Replace a specific word with associative array
  2. PHP File Copy File
  3. PHP File Copy Reverse
  4. PHP File Search within file
  5. PHP File Delete File
  6. PHP File Template
  1. PHP File Email Template
  2. PHP File Template with MySQL
  3. PHP File Access with MySQL
Navigation
Web
SQL
MISC
References