How do I use file_get_contents() when the filename contains a single quote?

1.1k views Asked by At

I have to read the contents of a file with a single quote (') in the file name. I have no influence on the file name, so renaming is not an option. Unfortunately, simply escaping it doesn't work, as in:

    $myFile = 'John\\\'s file';
    $text = file_get_contents($myFile);

What would be the correct way to access this file in PHP 5 on a Linux system?

1

There are 1 answers

1
rap-2-h On BEST ANSWER

You don't need to double-escape your string:

$myFile = 'John\'s file'; // This works fine.
$text = file_get_contents($myFile);

I've just tried a similar command on my terminal:

php -r "chdir('te\'st');"

It works.