HI, how to include relative path url in PHP. I am getting the following error. If i am giving complete path its working fine: Warning: main(../templates/dlinks_header.php?user_name=): failed to open stream: No such file or directory in /home/content/k/k/s/kkskrishna/html/dlinks/links/links.php on line 47
You should use require or require_once and then make sure you are going relative to the executing code not the first executed code. This is important when a file includes a file, includes a file and includes a file. Takes some tinkering on the server, but should be easy to figure out.
First things first: you say 'relative path URL'. I assume you mean 'relative path' as you should not be including via a URL unless you know exactly why you're doing that (as opposed to including via the filesystem). I think you'll find that when A includes B and B includes C, C is included in the context of A, not B. For what it's worth, I'm going to copy and paste a couple of my posts from http://forum.joomla.org/index.php/topic,49344 to here (I'm Narcissus over there, in case you want to follow the whole thread which may be useful). Anyway... Note that that outcome is on my PHP installation which was 4.something at the time. The same result has been replicated on a number of machines that I've been able to test this on. It may be different on PHP5 but that doesn't matter for the solution I use. Instead of using relative paths by themselves, I use a combination of relative paths, __FILE__ and dirname. So, if I wanted to do: include( '../../a_file.php' ); I would actually do: include( dirname( __FILE__ ) . '/../../a_file.php' ); So, when I'm developing I can use relative paths but the beauty is that in reality, I'm using what are essentially 'dynamic absolute paths'. You don't have any problems with including the wrong files and you don't have to worry about changing the directory names when you move your code to another server.