0

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

Ayaz Ali Shah
  • 3,453
  • 9
  • 36
  • 68

2 Answers2

3

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.

Community
  • 1
  • 1
T.Z.
  • 2,092
  • 1
  • 24
  • 39
1

Another option is to use the pre-defined constant DIRECTORY_SEPARATOR.

$__DIR = __DIR__ . DIRECTORY_SEPARATOR;

This way your code is more portable between operating systems, which may or may not be of concern to you.

vascowhite
  • 18,120
  • 9
  • 61
  • 77