-2

This should be simple but it doesn't work

  $photo = "../members/".$vid.".jpg";

  if (file_exists ($photo))
       echo "<img id='memberpic' src = '$photo'/>";

In this case the image is never shown.

However, if I leave out the if statement...

 echo "<img id='memberpic' src = '$photo'/>";

The image is displayed properly when the file exists but with an empty frame when it doesn't. I want to eliminate that empty frame.

What am I missing?

delpi767
  • 37
  • 6

3 Answers3

3

You're using a relative path for file_exists(). Try using the full server path instead.

e.g.

$photo = "/var/www/html/members/".$vid.".jpg";

but obviously replace with your server's full path :)

  • Use something like `__DIR__` or `$_SERVER['DOCUMENT_ROOT']` to turn a relative path into an absolute one. – SDC Jan 23 '13 at 16:42
  • ... or you can get the current working directory by `getcwd()` – ConcurrentHashMap Jan 23 '13 at 16:44
  • Thanks everyone but there seems to be something else at play here. I have switched to absolute paths and now File_exists is finding the file but the img tag is not. The following solution works but seems convuluted: $photo = "$root/members/".$vid.".jpg"; $photo1 = "../members/".$vid.".jpg"; if (file_exists ($photo)) echo ""; So, I'm let to believe that HTML wants a relative path but PHP prefers an absolute path. Can this be correct? – delpi767 Jan 23 '13 at 17:34
  • Ah, yes, sorry! You will need to redefine the variable for the image. You want to CHECK against the FULL PATH, but only link to your image using a relative path. Does that make sense? –  Jan 23 '13 at 17:35
  • Your webpage won't see the image using the full path, as that's not the same as the rendered URL of the web server. Hence the need to check the full filesystem path, and the relative public URL of the image. –  Jan 23 '13 at 17:37
  • Thanks monkeymatrix. Your comments make sense. It is working fine now. This seems like something I should have learned long ago.:) – delpi767 Jan 23 '13 at 17:40
0

Use absolute paths as often as you can. Relative paths are a pain for PHP to calculate them (in performance-wise).

And if you dont want to use Absolute path..you can use relative path with following.

$_SERVER['DOCUMENT_ROOT'] = '<root_dir_of_your_project>';  
file_exists($_SERVER['DOCUMENT_ROOT'].'/path/to/file.php');  
Rush
  • 740
  • 4
  • 13
0

There are some things you'll have to keep in mind while working with files and directory paths. Basically it's better to access files absolute (full path on the webserver) than relative (path from the directory the php-script is executed from). This will help you check if a file exists. You can get the active directory of your script with the PHP magic constant DIR (PHP Version >= 5.3) or dirname(FILE).

Another thing you have to keep track of is that your webservers document-root. Your (in your case) image files are addressed like: document-root: /var/www/example.com/img/photo.png url: http://example.com/img/photo.png

Although it is possible to use the "parent directory" with .. i would not recommend to use because it is hard.

unused
  • 796
  • 4
  • 12