Using/Giving absolute path

Hi

I wanted to use absolute path in includes … If if use

http://www.site.com/inc/myfile.php it gives me error.

What’s the best way to use it?

Most folk setup a constant to indicate a base then use this in their application.


<?php
define(
    'BASE_PATH',
    realpath(
        dirname(__FILE__)
    )
);

echo BASE_PATH; #/var/www/

?>

Note the difference between absolute path & absolute url.
You are using absolute url which are used for accessing via browser, esp used for css, images, js etc.
But in your case you have to use absolute path.
Note the AnthonySterling’s style of using absolute path.

Thanks

PHP Manual says:

If “URL fopen wrappers” are enabled in PHP (which they are in the default configuration), you can specify the file to be included using a URL (via HTTP or other supported wrapper - see List of Supported Protocols/Wrappers for a list of protocols) instead of a local pathname. If the target server interprets the target file as PHP code, variables may be passed to the included file using a URL request string as used with HTTP GET. This is not strictly speaking the same thing as including the file and having it inherit the parent file’s variable scope; the script is actually being run on the remote server and the result is then being included into the local script.

So it is possible to use the absolute URLs as well if URL fopen wrappers are enabled but in case of if the files are to be included from a remote server. But in case of same server, it is better to use Anthony’s style.

Anthony, why do you use realpath here? Any case when dirname fails?

As for URL fopen wrappers, it just never works as desired. It will never include PHP code itself, but just it’s output.
And, of course, must be turned off for security reasons.

For the question, PHPycho is absolutely right. OP is messing up filesystem paths and HTTP urls. Very common mistake for newbies. Filesystem basics must be learned for major operation systems. Absolute path starts from drive letter in windows and from a slash (“/”) in unix

Guys, I am not recommending to use it but I am just saying that it is possible to do so in against of the following in PHPycho’s post:

But in your case you have to use absolute path.

Nothing more than that. Indeed there rarely comes a situation to use the remote files to be included in a page.