Search code examples
phpfile-exists

How to add backslash next to php magic constants?


I'm trying to check if file exists or not. I tried simple http URL for the task but file_exists() does not support (my php version is 5.5.12 and allow url fopen is activated ).

So i tried in different way and its working, see below

if(file_exists(__DIR__.'\email_template.php')) {
    echo  'Template is available.';
}

Problem is that i'm adding template name dynamically and i need backslash between __DIR__ and $temp_name but i can't concatenate properly. I tried below

$__DIR = __DIR__.'\';
$__DIR = __DIR__.'"\"';
$__DIR =  __DIR__."\";

But no one is working, its return syntac error. So can anyone guide me how can i fix the issue i would like to appreciate. Thank you


Solution

  • Try this:

    $__DIR = __DIR__.'\\';
    

    Backshlash is special char in PHP (and other languages as well), that is used for noting that chars after them should be interpreted in special way - and is named "escape char". You do not want this to happen, so You should escape backshlas... By using one more backshlash. ;)

    You can read more here.