How to Get File Information in PHP

How to get file information in php - A file certainly has information about the file. Such file information such as file size, file type, file creation time and others. For that reason in this article I will submit a tutorial on how to get file information in php.
Some functions related to file information and manipulation are listed in the table as follows:


Function
Information
file_exists($file)
Check if $file exists or not
is_file($file)
Check if $file is a file or not.
is_dir($file)
Check if $ file is a directory or not.
is_executable($file)
Check if $ file includes files that can be run directly or not.
is_writable($file)
Check if $ file includes files that can be written or edited.
is_readable($file)
Check if $ file includes a readable file or not.
fileatime($file)
Generates the last access time of the file (unix timestamp).
filectime($file)
Generates file creation time (unix timestamp).
filemtime($file)
Generates the last modification time of the file (in unix timestamp).
filesize($file)
Generate large file size (in bytes).
filetype($file)
Generate file types.

To be able to understand more, here I give examples of programs to Get File Information using php.
Program 1
File Name: file11.php
Description: The program checks for the existence of a file.
Type the following php code into notepad.
<?php
$filename = "data.txt";

if (file_exists($filename)) {
echo "File $filename THERE IS";
} else {
echo "File $filename THERE IS NO";
}
?>

How to Get File Information in PHP

Save the php code with file11.php name in the htdocs folder. To run the program open the browser then type http://localhost/file11.php in the address bar then enter. The result will look like the following.
How to Get File Information in PHP
Program view

How to Get File Information in PHP
In windows explorer the file exists

Program 2
File Name: file12.php
Description: The program gets some file information.
Open notepad type the following php code.
<?php
$file = "data.txt";

if (is_file($file)) {
 echo "File <b>$file</b> Is FILE<br>"; 
 //
 if (is_executable ($file)) {   
 echo "File <b>$file</b> Can be run directly (executable)<br>";
 } else {   
 echo "File <b>$file</b> NO executable<br>"; 
 }
//
 if (is_writable ($file)) {   
 echo "File <b>$file</b> Can be written / diedit<br>";  
 } else {   
 echo "File <b>$file</b> NOT writable /  diedit<br>";
}
//
 if (is_readable ($file)) {   
 echo "File <b>$file</b> Can be read<br>";  
 } else {   
 echo "File <b>$file</b> NOT readable<br>"; 
 }
//
 echo "Access last file <b>$file</b> = ". date("d-m-Y  H:i:s.", fileatime($file)). "<br>";
 echo "File <b>$file</b> Created = ". date("d-m-Y H:i:s.",  filectime($file)). "<br>";
 echo "Last modified file <b>$file</b> = ". date("d- m-Y H:i:s.", filemtime($file)). "<br>";
 echo "File size <b>$file</b> = ". filesize($file). "  bytes<br>";
 echo "File type <b>$file</b> = ".filetype($file). "<br>"; 
 } else if (is_dir($file)){
 echo "File <b>$file</b> Is DIRECTORY<br>";  
 } else {
 echo "File <b>$file</b> Not known<br>"; 
 }
?>

How to Get File Information in PHP

Save the php code with the file12.php name in the htdocs folder. Run the program by opening the browser, then type http://localhost/file11.php in the address bar then enter. The result will look like below.
How to Get File Information in PHP
Program view

That's the tutorial how to get file information in php. Hopefully provide benefits and can improve our ability in learning php programming language. Learn also tutorial: How to Delete Directory Using PHP.

Comments