TrotskyIcepick Posted April 30, 2010 Share Posted April 30, 2010 Sorry about this, it's probably a really simple problem, but not for me being a newbie. I am trying to link to a stylesheet in PHP using the following code snippet: <?php function htmlpath($relative_path) { $realpath=realpath($relative_path); $htmlpath=str_replace($_SERVER['DOCUMENT_ROOT'],'',$realpath); return $htmlpath; } echo '<link href="',htmlpath('/stylesheet.css'),'" rel=stylesheet type=text/css />'; ?> However, this produces the following html output: <link href="" rel=stylesheet type=text/css /> No Request URL is produced in the header. If I remove the / before stylesheet.css, the following html is returned: <link href="test-www/stylesheet.css" rel=stylesheet type=text/css /> In the header, the Request URL is shown as http://localhost:8080/test-www/test-www/stylesheet.css I have tried echo '<link href="',htmlpath('../stylesheet.css'),'" rel=stylesheet type=text/css />'; but this produces: <link href="" rel=stylesheet type=text/css /> I can't think what else to try, I think I have a handle on absolute/relative paths, but this is simply stumping me. Thanks In Advance Andrew Jones Link to comment https://forums.phpfreaks.com/topic/200268-problem-referring-to-absolute-path-of-file-in-web-site/ Share on other sites More sharing options...
cags Posted April 30, 2010 Share Posted April 30, 2010 DOCUMENT_ROOT refers to a local path on your server, the href of a <link> tag expects a URL. What exactly is your objective? Link to comment https://forums.phpfreaks.com/topic/200268-problem-referring-to-absolute-path-of-file-in-web-site/#findComment-1050973 Share on other sites More sharing options...
TrotskyIcepick Posted April 30, 2010 Author Share Posted April 30, 2010 I found the code here http://theserverpages.com/php/manual/en/function.realpath.php I simply want to link to the stylesheet.css file. The output of the html is basically correct, just the path produced is wrong. Thanks Andrew Link to comment https://forums.phpfreaks.com/topic/200268-problem-referring-to-absolute-path-of-file-in-web-site/#findComment-1050978 Share on other sites More sharing options...
cags Posted April 30, 2010 Share Posted April 30, 2010 What is the URL of the stylesheet you wish to use and what is the URL of the page you wish to use it in? Link to comment https://forums.phpfreaks.com/topic/200268-problem-referring-to-absolute-path-of-file-in-web-site/#findComment-1051030 Share on other sites More sharing options...
TrotskyIcepick Posted April 30, 2010 Author Share Posted April 30, 2010 Stylesheet URL : http://localhost:8080/www-test/stylesheet.css URL of page that loads it : http://localhost:/8080/www-test/includes/header-menu.inc.php header-menu.inc.php is loaded by a php template http://localhost:8080/www-test/index.php using require once. I won't be back in work now until next Friday, so if you need any more information I can supply it then. TBH, if I was only using 1 php template there would not be an issue, I could simply use <link href="stylesheet.css" rel=stylesheet type=text/css />. The issue only arises because I load an almost identical php template from the /includes subdirectory and this makes the path to the stylesheet.css file incorrect. Thanks Andrew Link to comment https://forums.phpfreaks.com/topic/200268-problem-referring-to-absolute-path-of-file-in-web-site/#findComment-1051272 Share on other sites More sharing options...
TrotskyIcepick Posted May 7, 2010 Author Share Posted May 7, 2010 Just changed the way I was using php includes and now all works fine. I was simply over-thinking the problem. Andrew Link to comment https://forums.phpfreaks.com/topic/200268-problem-referring-to-absolute-path-of-file-in-web-site/#findComment-1054473 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.