View Full Version : file_exists() path problem

05-31-2011, 01:06 PM

I am using the file_exists() function, but since I use a wrong path, it is not working as supposed.

Here the code:

define ('IMGDIR', '/components/com_virtuemart/shop_image/product/');


The pictures are located here (as example only my-site.org):


path found in cpanel:

What path do I have to use for file_exists() that it is working?

05-31-2011, 01:29 PM
Is that all of your code? file_exists() returns a bool. It's intended to be used in conditions eg

// do some stuff

Also note i've never played with define, it may do some things i'm don't know of. Above code will work if it was just assigned as a normal variable.

05-31-2011, 03:47 PM
path found in cpanel:

What path do I have to use for file_exists() that it is working?

The same path as cpanel gives. You can't use a url with file_exists() as it is for detecting a file on the local file system or a network share file system.

05-31-2011, 05:30 PM
You can't use the same root folder in the PHP script that you would use in the HTML document because they don't use the same root.

Your path you are using points to the web root when accessed from the web (as a link would be). But when accessed server-side (as in a PHP script) you are not confined to the web root. You can go to levels of the file tree above the web root (which is what you are doing, only you're not coming back down to the web root to find your files).

The bottom line is that you need to make that root path factor-in the part of the file tree that is above the web root.

For example, if you have a PHP page located at http://www.example.com/some_folder/index.php and you were to run this code in your script some place convenient and see what it prints to the page:

print __FILE__;

You might expect to see something like "/some_folder/index.php". But what you would really see is something like "/home/user/public_html/some_folder/index.php"

In this example it is the "/home/user/public_html" part that you are missing from your root path that you would want to specify in the script.

So, depending on what your server's file tree is like, your path is probably supposed to be something like "/home/schmek/public_html/components/com_virtuemart/shop_image/product/".