PHP Basics
Functions in PHP
Working with Forms
Working with Files
Working with Databases
Advanced PHP Techniques

To get detailed file information in PHP, you can use a variety of built-in functions. Here’s an in-depth look at some of the most commonly used functions for this purpose:

1. file_exists()

This function checks whether a file or directory exists.

Syntax:

				
					bool file_exists(string $filename)

				
			

Example:

				
					if (file_exists('example.txt')) {
    echo 'File exists';
} else {
    echo 'File does not exist';
}

				
			

2. 'filesize()'

This function returns the size of a file in bytes.

Syntax:

				
					int filesize(string $filename)

				
			

Example:

				
					$size = filesize('example.txt');
echo "File size: $size bytes";

				
			

3. 'filemtime()'

This function returns the last modification time of a file as a Unix timestamp

Syntax:

				
					int filemtime(string $filename)

				
			

Example:

				
					$modTime = filemtime('example.txt');
echo "Last modified: " . date("F d Y H:i:s.", $modTime);

				
			

4. 'fileperms()'

This function returns the file permissions.

Syntax:

				
					int fileperms(string $filename)

				
			

Example:

				
					$perms = fileperms('example.txt');
echo substr(sprintf('%o', $perms), -4); // Outputs file permissions in octal

				
			

5. filetype()'

This function returns the type of the file.

Syntax:

				
					string filetype(string $filename)

				
			

Example:

				
					$type = filetype('example.txt');
echo "File type: $type"; // Outputs 'file', 'dir', 'link', etc.

				
			

6. 'finfo_open() and finfo_file()'

These functions are used to determine the MIME type of a file.

Syntax:

				
					resource finfo_open([int $options = FILEINFO_NONE [, string $magic_file = NULL]])
string finfo_file(resource $finfo, string $file_name)

				
			

Example:

				
					$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($finfo, 'example.txt');
echo "MIME type: $mime";
finfo_close($finfo);

				
			

7. 'stat()'

This function gives information about a file, returning an array with details like size, last access time, modification time, etc.

Syntax:

				
					array stat(string $filename)

				
			

Example:

				
					$info = stat('example.txt');
print_r($info);

				
			

8. 'is_readable()', 'is_writable()', and 'is_executable()'

These functions check whether a file is readable, writable, or executable, respectively.

Syntax:

				
					bool is_readable(string $filename)
bool is_writable(string $filename)
bool is_executable(string $filename)

				
			

Example:

				
					if (is_readable('example.txt')) {
    echo 'File is readable';
}

if (is_writable('example.txt')) {
    echo 'File is writable';
}

if (is_executable('example.txt')) {
    echo 'File is executable';
}

				
			

Comprehensive Example

Here is a comprehensive example that combines several of the above functions to get detailed information about a file:

				
					$file = 'example.txt';

if (file_exists($file)) {
    echo "File: $file\n";
    echo "Size: " . filesize($file) . " bytes\n";
    echo "Type: " . filetype($file) . "\n";
    echo "Permissions: " . substr(sprintf('%o', fileperms($file)), -4) . "\n";
    echo "Last modified: " . date("F d Y H:i:s.", filemtime($file)) . "\n";
    echo "Is readable: " . (is_readable($file) ? 'Yes' : 'No') . "\n";
    echo "Is writable: " . (is_writable($file) ? 'Yes' : 'No') . "\n";
    echo "Is executable: " . (is_executable($file) ? 'Yes' : 'No') . "\n";

    $finfo = finfo_open(FILEINFO_MIME_TYPE);
    echo "MIME type: " . finfo_file($finfo, $file) . "\n";
    finfo_close($finfo);
} else {
    echo "File does not exist.";
}

				
			

These functions allow you to retrieve and display a wide range of file information in PHP. By combining them, you can create detailed reports about files and their properties, which is useful for tasks such as file management, security checks, and content delivery applications.

Scroll to Top