6

I tried the following code to check existence of file in root.

if($res['profile_picture']!="" && file_exists("images/".$res['users_id']."/thumnails/".$res['profile_picture'])){
    $photo_p="images/".$res['users_id']."/thumnails/".$res['profile_picture'];
}

It works only on root directory not sub directory.

I'm not sure whether function file_exist checks for both absolute and relative paths so I tried adding ROOT and $_SERVER['DOCUMENT_ROOT']. But still it didn't worked out.

Any Help?

Brad Werth
  • 17,411
  • 10
  • 63
  • 88
Mohit Tripathi
  • 127
  • 1
  • 7

2 Answers2

4

For code portability I suggest you always use absolute paths in functions like file_exists(), otherwise you may wind up breaking your head when including multiple files in different directories and/or running in CLI mode.

ROOT constant may be undefined in your code. Also, $_SERVER['DOCUMENT_ROOT'] in some circumstances cannot be relied on, i.e. when you use vhost_alias apache module.

Generally,

file_exists("{$_SERVER['DOCUMENT_ROOT']}/images/{$res['users_id']}/thumnails/{$res['profile_picture']}")

should work for you.

ashein
  • 477
  • 2
  • 12
0

The function file_exists() checks exactly the path you give to it. There is no fancy magic in there.

Hand it an absolute path and it will check that path. Hand it a relative path and it will check from the current working directory. You might want to think about what actually is your working directory.

arkascha
  • 41,620
  • 7
  • 58
  • 90