Search code examples
phpimagelaravel-5special-characters

Laravel File Exists function is not working when filename contains special characters


I have a image with the name The-seven-Churches-of-the-Revelation-&-Istanbul.jpg

However, the filename is saved in the database in this format; The-seven-Churches-of-the-Revelation-%26-Istanbul.jpg

I am using the following code, and also ignoring space

$cover = str_replace('%20', ' ', $cover);

if (!\Illuminate\Support\Facades\File::exists(base_path($cover))) :
            echo "file not found";
endif;

Result is

file not found

How can I deal with the special characters in the filenames?


Solution

  • It seems you may be having issues with the URL Encoded values, so urldecode may be your friend. You can see the docs for this here

    $cover = urldecode($cover);
    
    if (!\Illuminate\Support\Facades\File::exists(base_path($cover))) :
                echo "file not found";
    endif