I have the following directory structure:
/webroot
/static
/img
/css
- style.css
/js
/pdf
/mp3
/mp4
- index.php
In my 'index.php' I have the following code:
// define the frontend path constant
$frontend = realpath(dirname(__FILE__));
define('__FRONTEND__', $frontend);
I have my templates in the backend (outside webroot) and when I code something like this:
<link type="text/css"
rel="stylesheet"
href="<?php echo __FRONTEND__; ?>/static/css/style.css"
/>
The CSS code is not shown. I don’t get a warning either. When I look in the source I see:
<link type="text/css"
rel="stylesheet"
href="C:\xampp\htdocs\projectx\webroot/static/css/style.css"
/>
Here you see the different slashes: '\' vs '/'. Strangely, using the same kind of structure doesn’t fail on my backend constant '__BACKEND__' which I also have in my 'index.php':
// define the frontend path constant
$backend = realpath(dirname(__FILE__) . '/../backend');
define('__BACKEND__', $backend);
Why does my '__FRONTEND__' constant fail?
You shouldn’t use
realpath(dirname(__FILE__))in your templates. It’s the system’s path, not the one that your users will have access to.If your web root is webroot/ as you say, then
href="/static/css/style.css"will do the trick.Also, your problem has nothing to do with slashes and backslashes, both work in Windows.