Finding out info about a file
In this example we will use several of php built in file functions to display various bits of info about a file , we will find the file type , get the size of a file , check if a file is a directory , check if its executable , check whether you can read and write to afile .
Code :
<?php
//various file operations
//change this file to one on your computer
$filename = "moreover.txt";
//get the file type
$type_of_file = filetype("$filename");
//get the size of the file
$size_of_file = filesize("$filename");
//is the file a directory
$isadir = is_dir($filename);
//is the file executable
$isexec = is_executable($filename);
//is file readable
$isread = is_readable($filename);
//is file writable
$iswrite = is_writable($filename);
//display the file type
echo "The file type is $type_of_file<br>";
//display the file size
echo "The file size is $size_of_file bytes<br>";
//is it a directory
echo "Is the file a directory (0 = false , 1 is true) : $isadir<br>";
//is file executable
echo "Is the file executable (0 = false , 1 is true) : $isexec<br>";
//is file readable
echo "Is the file readable (0 = false , 1 is true) : $isread<br>";
//is file writable
echo "Is the file writable (0 = false , 1 is true) : $iswrite<br>";
?>
Example :
The file type is file
The file size is 23831 bytes
Is the file a directory (0 = false , 1 is true) :
Is the file executable (0 = false , 1 is true) :
Is the file readable (0 = false , 1 is true) : 1
Is the file writable (0 = false , 1 is true) : 1