Navigation

Php

How to Get File Information with pathinfo()

Extract file path components like directory, filename, and extension using PHP's pathinfo() function for robust file handling.

Table Of Contents

Working Solution

<?php

// Basic pathinfo usage
$filepath = '/var/www/html/uploads/document.pdf';
$info = pathinfo($filepath);

print_r($info);
/* Output:
Array (
    [dirname] => /var/www/html/uploads
    [basename] => document.pdf
    [extension] => pdf
    [filename] => document
)
*/

// Get specific components
$directory = pathinfo($filepath, PATHINFO_DIRNAME);     // '/var/www/html/uploads'
$filename = pathinfo($filepath, PATHINFO_FILENAME);     // 'document'
$extension = pathinfo($filepath, PATHINFO_EXTENSION);   // 'pdf'
$basename = pathinfo($filepath, PATHINFO_BASENAME);     // 'document.pdf'

// File upload validation
function validateUploadedFile(string $originalName): array {
    $info = pathinfo($originalName);
    
    $allowedExtensions = ['jpg', 'jpeg', 'png', 'gif', 'pdf', 'doc', 'docx'];
    $maxFilenameLength = 100;
    
    $validation = [
        'valid' => true,
        'errors' => []
    ];
    
    // Check extension
    if (!isset($info['extension']) || !in_array(strtolower($info['extension']), $allowedExtensions)) {
        $validation['valid'] = false;
        $validation['errors'][] = 'Invalid file extension';
    }
    
    // Check filename length
    if (strlen($info['filename']) > $maxFilenameLength) {
        $validation['valid'] = false;
        $validation['errors'][] = 'Filename too long';
    }
    
    // Check for dangerous characters
    if (preg_match('/[^a-zA-Z0-9._-]/', $info['filename'])) {
        $validation['valid'] = false;
        $validation['errors'][] = 'Filename contains invalid characters';
    }
    
    return $validation;
}

// Generate unique filename while preserving extension
function generateUniqueFilename(string $originalPath, string $targetDirectory): string {
    $info = pathinfo($originalPath);
    $extension = isset($info['extension']) ? '.' . $info['extension'] : '';
    $basename = $info['filename'];
    
    $counter = 1;
    $newFilename = $basename . $extension;
    
    while (file_exists($targetDirectory . '/' . $newFilename)) {
        $newFilename = $basename . '_' . $counter . $extension;
        $counter++;
    }
    
    return $newFilename;
}

// File organization by extension
function organizeFilesByType(array $filePaths): array {
    $organized = [
        'images' => [],
        'documents' => [],
        'archives' => [],
        'other' => []
    ];
    
    $imageExts = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'svg'];
    $docExts = ['pdf', 'doc', 'docx', 'txt', 'rtf', 'odt'];
    $archiveExts = ['zip', 'rar', '7z', 'tar', 'gz'];
    
    foreach ($filePaths as $filepath) {
        $extension = strtolower(pathinfo($filepath, PATHINFO_EXTENSION));
        
        if (in_array($extension, $imageExts)) {
            $organized['images'][] = $filepath;
        } elseif (in_array($extension, $docExts)) {
            $organized['documents'][] = $filepath;
        } elseif (in_array($extension, $archiveExts)) {
            $organized['archives'][] = $filepath;
        } else {
            $organized['other'][] = $filepath;
        }
    }
    
    return $organized;
}

// Safe filename sanitization
function sanitizeFilename(string $filename): string {
    $info = pathinfo($filename);
    $extension = isset($info['extension']) ? '.' . $info['extension'] : '';
    
    // Remove directory path (security measure)
    $cleanName = $info['filename'];
    
    // Replace dangerous characters
    $cleanName = preg_replace('/[^a-zA-Z0-9._-]/', '_', $cleanName);
    
    // Remove multiple consecutive underscores
    $cleanName = preg_replace('/_+/', '_', $cleanName);
    
    // Trim underscores from start/end
    $cleanName = trim($cleanName, '_');
    
    // Ensure we have a name
    if (empty($cleanName)) {
        $cleanName = 'file_' . time();
    }
    
    return $cleanName . $extension;
}

// Create backup filename
function createBackupFilename(string $originalPath): string {
    $info = pathinfo($originalPath);
    $timestamp = date('Y-m-d_H-i-s');
    
    $backupName = $info['filename'] . '_backup_' . $timestamp;
    if (isset($info['extension'])) {
        $backupName .= '.' . $info['extension'];
    }
    
    return $info['dirname'] . '/' . $backupName;
}

// Usage examples
$uploadedFile = $_FILES['document']['name'] ?? 'test-file.pdf';

// Validate uploaded file
$validation = validateUploadedFile($uploadedFile);
if ($validation['valid']) {
    echo "File is valid\n";
} else {
    echo "Errors: " . implode(', ', $validation['errors']) . "\n";
}

// Generate unique filename
$uniqueName = generateUniqueFilename($uploadedFile, '/uploads');
echo "Unique filename: $uniqueName\n";

// Organize files
$files = [
    '/docs/report.pdf',
    '/images/photo.jpg',
    '/data/archive.zip',
    '/misc/readme.txt'
];
$organized = organizeFilesByType($files);
print_r($organized);

// Sanitize filename
$unsafeFilename = '../../../etc/passwd.txt';
$safeFilename = sanitizeFilename($unsafeFilename);
echo "Safe filename: $safeFilename\n"; // 'passwd.txt'

// Create backup
$backupPath = createBackupFilename('/config/settings.json');
echo "Backup path: $backupPath\n";

Behind the Scenes

pathinfo() breaks down file paths into manageable components:

  1. Path Parsing: Separates directory, filename, and extension
  2. Cross-Platform: Works on Windows, Linux, and macOS paths
  3. Safe Extraction: Handles edge cases like files without extensions
  4. Component Access: Get specific parts without manual string manipulation

Available Components:

  • PATHINFO_DIRNAME - Directory path
  • PATHINFO_BASENAME - Full filename with extension
  • PATHINFO_EXTENSION - File extension only
  • PATHINFO_FILENAME - Filename without extension

Essential for file uploads, path manipulation, and file organization tasks where you need to work with specific parts of file paths.

Share this article

Add Comment

No comments yet. Be the first to comment!

More from Php