Search code examples
phpalgorithmcharacterfilenamesfile-exists

file_exists() for a file that contains (&) in filename


$filename = "Sara & I.mp3";
if (file_exists($filename)) {
 ...
}

If the filename has a "&" PHP does not return true for file_exists

How can I escape "&" in the $filename for the file_exists to work?

Already tried urlecode(), urlrawencode(), htmlentities(), and escaping the '&' with a backslash (\&)... no go

ps. this is on a linux system running RedHat5

thanks in advance


Solution

  • The script seems to work for me.

    Make sure that the file you're checking is in the directory where your script is run.

    use getcwd() to see where your script is running.

    e.g. if your script is in my/scripts/script.php but if it's included and invoked by another script at my/other/scripts/index.php then your script would actually be running in the my/other/scripts directory, NOT in the my/scripts directory as you might expect