Attaching a stylesheet with dirname(__FILE__) won't work even though the file is there
I have a PHP class that creates a header for my html file.. It's included, and that file is included again.. So I thought that the dirname(__FILE__)
function could work.. But it says it can't find the stylesheet..
I'm using mamp on os x, and when I take the path that I get from dirname(__FILE__)./../stylesheets/stylesheet.css
into the terminal, the file is found.. I'm pretty sure the path is correct.
What can be the reason for this? I use dirname(开发者_Python百科__FILE__)
all the time when I include files, that works..
Thanks
EDIT: Files and directories: /data/main.php /stylesheets/stylesheet.css /public/index.php In the main.php: public function createHeader(){ `$stylesheetpath = dirname(__FILE__) . "/../stylesheets/stylesheet.css";` `$header = "\n";` return $header }
The relative path should be ok.
<LINK REL=StyleSheet HREF="../stylesheets/stylesheet.css" TYPE="text/css">
An absolute path here would be absolute with respect to the document root, not the file system.
dirname(__FILE__)
is appropriate for including script files server-side, but not for paths used by the client. Simply put, the link tag is an instruction to the browser to go ahead and request that file.
From you file structure, /stylesheets/
is out of the public reach, it must be placed inside the /public/
folder for the browsers to retrieve the files:
/data/main.php
/public/stylesheets/stylesheet.css
/public/index.php
Even when the file main.php
is working, the file being viewed by the browser is index.php
, so the relative path would be stylesheets/stylesheet.css
edit: read the question incorrectly...
use
<link rel="stylesheet" href="/stylesheets/stylesheet.css" type="text/css"/>
for css
Is this a problem with partial case-sensitivity on Mac OSX?
精彩评论