What are some common mistakes to avoid when using pathinfo() in PHP to extract file information from URLs?

One common mistake to avoid when using pathinfo() in PHP to extract file information from URLs is not checking if the file exists before attempting to extract information. This can lead to errors if the file does not exist. To solve this issue, you should first check if the file exists using file_exists() before calling pathinfo().

$url = 'https://www.example.com/images/photo.jpg';

if (file_exists($url)) {
    $fileInfo = pathinfo($url);
    
    echo 'Filename: ' . $fileInfo['filename'] . PHP_EOL;
    echo 'Extension: ' . $fileInfo['extension'] . PHP_EOL;
    echo 'Basename: ' . $fileInfo['basename'] . PHP_EOL;
} else {
    echo 'File does not exist.';
}