tags:

views:

36

answers:

4

I store user images in "images" forlder and the full path is something like this:

http://example.com/images/imagename.jpg

When I try to get the image path like this:

$path = 'images/imagename.jpg';

it works fine on home page, but since I have different permalink structure it doesn't work on other pages. It becames something like http://example.com/something/images/imagename.jpg

Of course I can write:

$path = 'http://example.com/images/imagename.jpg';

but in this case

file_exists($path)

always returns false even if the file exists. It's a problem!

Can anyone please help?

A: 

I would use parse-url and get the "path" argument as following:

<?php
  $url_tab = parse_url($path);
  echo $url_tab['path'];
?>

So you can keep the full version and get the path argument.

Otherwize, you can keep the absolute path, and use the $_SERVER['SERVER_NAME'] to concatenate the path and get the full url.

Aif
+3  A: 

Try $path = '/images/imagename.jpg; that way you're are referencing from the document root path.

Tapdingo
A: 

file_exists should check for existance of file in filesystem, while html images should use url (whether they are relative or absolute). There should be no confusion between to, and as you noticed it using same variable for both could lead to issues.

You can use some constants to define you application root path. If you use a front controller at the root dir of your project

define('APP_PATH', dirname(__FILE__));

Or you can use little helper functions to get your image path and url separately, something to use in your view/template files

get_image_path($fileName);
get_image_uri($fileName);

this way you can easily move images files elsewhere and keep your existants url the same, or change urls without actually moving files in file system.

Benoit
A: 

You've almost got it. path ought to be absolute (i.e. starting from /) from the web root.
so, to check existence of the file, you have to extend it's path to absolute from the filesystem root.

$path = '/images/imagename.jpg';
if (file_exists($_SERVER['SERVER_NAME'].$path)) {
  echo "<img src=\"$path\">";
}

So, this would work for any part of the site and pass existence check. Always stuck with absolute paths, it will never fail you.

Col. Shrapnel
file_exists($path) still returns false but when I use echo "<img src=\"$path\">"; the image is displayed properly... Can anyone please explain why?
Levani
@Levani because there is no *file* /images/imagename.jpg in your **filesystem.** Go figure.
Col. Shrapnel
I'm a bit confused... :) The file "imagename.jpg" exists in folder "images", right? What else does the file_exists() function check?
Levani
@Levani it need to know where to check. so it needs full **path,** not filename only. and path consists of filename itself and a directory. And `images` and `/images` are two **different** directories. First one is relative and latter one is absolute. first one have path like `/home/www/site/html/images` while latter is still `/images` and doesnt exists. Doesnt exists in the *filesystem* I am too tired to explain such basic things as what filesystem is, but if you ask another question, you'll be stuffed with other answers for sure.
Col. Shrapnel