info()

public function info(string $file_path): array

Description

Retrieves metadata about a file, including its size, last modification time, permissions, and MIME type.

Parameters

ParameterTypeDescription
$file_path string The path to the file.

Return Value Details

The method returns an associative array containing the following keys:

KeyTypeDescription
file_name string The filename without the directory path (e.g., "document.pdf").
size int File size in bytes.
human_readable_size string Formatted file size (e.g., "2.45 MB").
modified_time int Last modification time as a Unix timestamp.
permissions int Raw file permissions as an integer.
readable_permissions string Formatted permissions in octal notation (e.g., "0644").
mime_type string The MIME type of the file (e.g., "application/pdf").

Exception Handling: This method throws an exception if the file does not exist. Always verify the file exists using $this->file->exists($file_path) before calling info().

Example #1

The code sample below demonstrates how to display detailed file information on a document management page.

PHP
public function view_document_details(): void {
    $document_id = segment(3, 'int');
    
    // Get document record
    $document = $this->db->get_where($document_id, 'documents');
    
    if ($document === false) {
        redirect('documents/not_found');
    }
    
    $file_path = 'modules/documents/storage/' . $document->filename;
    
    // Check if file exists before getting info
    if ($this->file->exists($file_path)) {
        $file_info = $this->file->info($file_path);
        
        $data['document'] = $document;
        $data['file_info'] = $file_info;
        $this->view('document_details', $data);
    } else {
        redirect('documents/not_found');
    }
}

In the view file, you can display the file information:

View File
<h2><?= out($document->title) ?></h2>

<div class="file-details">
    <p><strong>Filename:</strong> <?= out($file_info['file_name']) ?></p>
    <p><strong>File Size:</strong> <?= out($file_info['human_readable_size']) ?></p>
    <p><strong>File Type:</strong> <?= out($file_info['mime_type']) ?></p>
    <p><strong>Last Modified:</strong> <?= date('F j, Y g:i A', $file_info['modified_time']) ?></p>
    <p><strong>Permissions:</strong> <?= out($file_info['readable_permissions']) ?></p>
</div>

Example #2

The example above shows how to use file information to implement a storage quota system.

PHP
public function check_user_storage(): array {
    $user_id = segment(3, 'int');
    $user_directory = 'modules/files/storage/user_' . $user_id;
    
    $total_size = 0;
    $file_count = 0;
    
    if ($this->file->exists($user_directory)) {
        $contents = $this->file->list_directory($user_directory);
        
        foreach ($contents['files'] as $file) {
            $file_path = $user_directory . '/' . $file['name'];
            
            if ($this->file->exists($file_path)) {
                $info = $this->file->info($file_path);
                $total_size += $info['size'];
                $file_count++;
            }
        }
    }
    
    // Calculate storage usage
    $quota_bytes = 100 * 1024 * 1024; // 100 MB quota
    $usage_percent = ($total_size / $quota_bytes) * 100;
    
    // Format for display
    $sizes = ['B', 'KB', 'MB', 'GB'];
    $factor = floor((strlen($total_size) - 1) / 3);
    $human_readable = sprintf("%.2f", $total_size / pow(1024, $factor)) . ' ' . $sizes[$factor];
    
    return [
        'file_count' => $file_count,
        'total_size_bytes' => $total_size,
        'total_size_formatted' => $human_readable,
        'usage_percent' => round($usage_percent, 2),
        'quota_exceeded' => $total_size > $quota_bytes
    ];
}

Example #3

The example above demonstrates how to filter and display files by type using MIME information.

PHP
public function list_files_by_type(): void {
    $directory = 'modules/uploads/documents';
    $file_type = segment(3); // e.g., 'pdf', 'image', 'text'
    
    $filtered_files = [];
    
    if ($this->file->exists($directory)) {
        $contents = $this->file->list_directory($directory);
        
        foreach ($contents['files'] as $file) {
            $file_path = $directory . '/' . $file['name'];
            
            if ($this->file->exists($file_path)) {
                $info = $this->file->info($file_path);
                
                // Filter by MIME type category
                $mime_type = $info['mime_type'];
                $matches = false;
                
                if ($file_type === 'pdf' && $mime_type === 'application/pdf') {
                    $matches = true;
                } elseif ($file_type === 'image' && strpos($mime_type, 'image/') === 0) {
                    $matches = true;
                } elseif ($file_type === 'text' && strpos($mime_type, 'text/') === 0) {
                    $matches = true;
                }
                
                if ($matches) {
                    $filtered_files[] = [
                        'name' => $info['file_name'],
                        'size' => $info['human_readable_size'],
                        'mime_type' => $info['mime_type'],
                        'modified' => date('Y-m-d H:i:s', $info['modified_time'])
                    ];
                }
            }
        }
    }
    
    $data['files'] = $filtered_files;
    $data['file_type'] = $file_type;
    $this->view('files_by_type', $data);
}

Working with Timestamps: The modified_time value is returned as a Unix timestamp (number of seconds since January 1, 1970). Use PHP's date() function to convert it to a human-readable format: date('F j, Y g:i A', $file_info['modified_time']) produces output like "December 26, 2024 3:45 PM".