PHP Tutorial
PHP Flow Control
PHP Functions
PHP String
PHP Array
PHP Date Time
PHP Object Oriented
Regular Expression
PHP Cookie & Session
PHP Error & Exception handling
MySQL in PHP
PHP File Directory
PHP Image Processing
In PHP, you can use built-in functions to get various attributes of a file, such as its size, type, last modification time, etc. Here's a simple tutorial showing how to retrieve different file attributes in PHP:
1. Get File Size
You can use the filesize()
function to get the size of a file. The function returns the size of the file in bytes.
<?php $filename = 'myfile.txt'; echo filesize($filename); ?>
2. Get File Type
The filetype()
function returns the type of the file. It can return values like file
, dir
, link
, etc.
<?php $filename = 'myfile.txt'; echo filetype($filename); ?>
3. Get File Last Access Time
The fileatime()
function returns the last access time of a file. It returns a Unix timestamp.
<?php $filename = 'myfile.txt'; echo date("F d Y H:i:s.", fileatime($filename)); ?>
4. Get File Last Modification Time
The filemtime()
function returns the last modification time of a file. It returns a Unix timestamp.
<?php $filename = 'myfile.txt'; echo date("F d Y H:i:s.", filemtime($filename)); ?>
5. Check if File is Writable
The is_writable()
function checks whether a file is writable or not. It returns TRUE
if the file is writable, and FALSE
otherwise.
<?php $filename = 'myfile.txt'; if (is_writable($filename)) { echo 'The file is writable'; } else { echo 'The file is not writable'; } ?>
6. Check if File is Readable
The is_readable()
function checks whether a file is readable or not. It returns TRUE
if the file is readable, and FALSE
otherwise.
<?php $filename = 'myfile.txt'; if (is_readable($filename)) { echo 'The file is readable'; } else { echo 'The file is not readable'; } ?>
These are some of the functions you can use to get file attributes in PHP. Remember to always check if the file exists using the file_exists()
function before trying to access its attributes to prevent errors.
Retrieving file size in PHP:
Get the file size using filesize()
:
<?php $filename = 'example.txt'; $filesize = filesize($filename); echo "File size: $filesize bytes";
Getting file permissions with PHP:
Get file permissions using fileperms()
:
<?php $filename = 'example.txt'; $permissions = fileperms($filename); echo "File permissions: $permissions";
PHP file last modified timestamp:
Get the last modified timestamp using filemtime()
:
<?php $filename = 'example.txt'; $lastModified = filemtime($filename); echo "Last modified: " . date('Y-m-d H:i:s', $lastModified);
File owner and group information in PHP:
Get file owner and group using fileowner()
and filegroup()
:
<?php $filename = 'example.txt'; $owner = fileowner($filename); $group = filegroup($filename); echo "File owner: $owner, Group: $group";
Checking if a file is readable in PHP:
Check if a file is readable using is_readable()
:
<?php $filename = 'example.txt'; $isReadable = is_readable($filename); echo "Is readable: " . ($isReadable ? 'Yes' : 'No');
PHP check if file is writable:
Check if a file is writable using is_writable()
:
<?php $filename = 'example.txt'; $isWritable = is_writable($filename); echo "Is writable: " . ($isWritable ? 'Yes' : 'No');
Determining file type with PHP:
Determine the file type using filetype()
:
<?php $filename = 'example.txt'; $fileType = filetype($filename); echo "File type: $fileType";
Getting file extension in PHP:
Get the file extension using pathinfo()
:
<?php $filename = 'example.txt'; $extension = pathinfo($filename, PATHINFO_EXTENSION); echo "File extension: $extension";
Retrieving file creation time in PHP:
Retrieve file creation time using filectime()
:
<?php $filename = 'example.txt'; $creationTime = filectime($filename); echo "Creation time: " . date('Y-m-d H:i:s', $creationTime);
PHP file exists and is a file check:
Check if a file exists and is a regular file using file_exists()
and is_file()
:
<?php $filename = 'example.txt'; $exists = file_exists($filename); $isFile = is_file($filename); echo "File exists: " . ($exists ? 'Yes' : 'No') . ", Is a file: " . ($isFile ? 'Yes' : 'No');
Checking if a file is a directory in PHP:
Check if a file is a directory using is_dir()
:
<?php $filename = 'example_directory'; $isDirectory = is_dir($filename); echo "Is a directory: " . ($isDirectory ? 'Yes' : 'No');
PHP is_file()
vs is_dir()
functions:
Understand the difference between is_file()
and is_dir()
:
<?php $filename = 'example.txt'; $isFile = is_file($filename); $isDirectory = is_dir($filename); echo "Is a file: " . ($isFile ? 'Yes' : 'No') . ", Is a directory: " . ($isDirectory ? 'Yes' : 'No');
File information with PHP stat()
function:
Get detailed file information using stat()
:
<?php $filename = 'example.txt'; $fileInfo = stat($filename); echo "File information: "; print_r($fileInfo);