Getting File Information

Contact Us or call 1-877-932-8228
Getting File Information

Getting File Information

The following code sample illustrates how to get information about a file using PHP.

Code Sample:

Files/Demos/FileInfo.php
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>File Details</title>
</head>
<body>
<?php
	$currentDir = 'Resumes/';
	$file = basename('J_C_Resume.txt'); 	
	echo '<h1>Details of file: ' . $file.'</h1>'; 
	$file = $currentDir.$file;
	
	echo '<h2>File data</h2>';
	echo 'File last accessed: ' . 
			date('j F Y H:i', fileatime($file)) . '<br>';
	echo 'File last modified: ' .
			date('j F Y H:i', filemtime($file)).'<br>';
	echo 'File type: ' . filetype($file).'<br>';
	echo 'File size: '.filesize($file).' bytes<br>';
	
	echo '<h2>File tests</h2>';
	echo 'is_dir: ' .
		(is_dir($file)? 'true' : 'false') . '<br>';
	echo 'is_file: ' .
		(is_file($file)? 'true' : 'false').'<br>';
	echo 'is_readable: ' . 
		(is_readable($file)? 'true' : 'false').'<br>';
	echo 'is_writable: ' . 
		(is_writable($file)? 'true' : 'false').'<br>';
?>
</body>
</html>

The functions used in this script are described in the following table.

Function Description
basename() Strips off the path and returns the file name.
fileatime() Returns the last accessed time of the file.
filemtime() Returns the last modified time of the file.
filetype() Returns the type of file (e.g, file or dir).
filesize() Returns the size of the file in bytes.
is_dir() Returns true if the passed value is a directory, false if it isn't.
is_file() Returns true if the passed value is a file, false if it isn't.
is_readable() Returns true if the file is readable, false if it isn't.
is_writable() Returns true if the file is writable, false if it isn't.
Next