Jump to content

Problem referring to absolute path of file in web site


TrotskyIcepick

Recommended Posts

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.