I would like to know whether there's an easy common way to include a css file, which is in a folder, from a html/php file inside another folder?
The structure looks like this:
index.php
about
index.php
css
style.css
What I want is to know how to get the correct path to the style.css file inside the css folder from the index.php hidden in that about directory.
This is going in about/index.php?
<link rel="stylesheet" href="../css/style.css" />
.. basically says, "Go up one level from where this script is located."
Related
My folders are here:
In my header.php there is this code:
<link rel="stylesheet" href="style.css">
In the index.php i can include like this:
include('header.php')
I can reach style.css there is no problem.
But, in the product/product.php
include('../header.php')
I can not reach style.css
I can reach the header.php but i can not reach style.css. Because in the header.php, this include code seems that style.css is in the product/style.css, but my style.css is in home folder. How can i reach? Can u help me?
The solution is simple the browsers try to request the file relative to the current directory, so just need to add / in the begging of the HREF to make sure you are referring to a file that is in the root.
Solution:
<link rel="stylesheet" href="/style.css">
I created a index.php file that's uses <?php require("sidebar.html"); ?> to include a sidebar; the HTML element of the sidebar shows, however the css styling isn't showing. I've search Google and tried different method but it's not showing, any help would be highly appreciated.
The sidebar.html is located in HTML/ folder. And index.php is located in root/ folder
The css styling for the sidebar is being reference within the sidebar.html file
My css file is located in CSS/ folder
new to web development; Trying to make a sidebar that I can call on every page instead of hard-coding it to every page.
When you include an HTML file into a PHP script, path to all the related files (i.e. files that are referenced in the HTML document) must be relative to the PHP script in which you have included the HTML.
Have a look at the file structure below:
Home
index.php
Includes
Assets
style.css
action.js
header.html
The Assets directory contains CSS and JS files which are included in header.html. Now, if header.html has to be included in index.php that is inside the Home directory, the src/href attributes need to point to the path of css/js files relative to index.php.
Something like this:
<link rel="stylesheet" href="../Includes/Assets/style.css" />
Happy coding :)
Prepare a BASE URL at the top of the page like below (based on your project directory location).
Try
$baseURL = "http://".$_SERVER['SERVER_NAME'];
or
$baseURL = "http://".$_SERVER['SERVER_NAME']."/your_project_dir/";
Use HTML base tag in your <head> tag before your<link> tags like
<html>
<head>
<title>Project Title</title>
<base href="<?php echo $baseURL;">
<link href="your_css_file.css" type="text/css">
</head>
<body>
page content
</body>
</html>
Cross check your "your_css_file.css" location (like css/your_css_file.css or styles/your_css_file.css ...)
Try to add
<style>
...
</style>
In the first and end of your css require file, the css will read as internal stylesheet. And change the extension .css to
.php
I have a question related to php include.
I have a folder called login and another folder files. I have a footer.php in files and its stylesheet lies also in same folder as style.css.
so , I want to include footer.php in login.php as <?php include('../files/footer.php');?> but its css is missing. how can I solve this problem?
You make your login.php extend some layout.php that has all the required CSS files attached.
When you write your html code, use the complete url (start with http://) to your css and js file in the <head>. This way you will not have problems about relative paths
In the head, you should use an absolute path for your css link:
<link rel="stylesheet" href="http://localhost/your-main-folder/files/footer.css" />
or you can use this:
<base href="http://localhost/your-main-folder/" />
<link rel="stylesheet" href="files/footer.css" />
i have two files:
1.- root/folder/folder/themes/themeindex.php
and
2.- root/index.php
I want to include themeindex.php in index.php so when you enter to root directory, it will load the theme without taking you to (or showing you) the root/folder/folder/themes/ path.
I'm struggling to find or figure out a way to include the themeindex.php file but keeping the URLs local to its themes folder.
Meaning my
<link rel="stylesheet" type="text/css" href="style.css" />
will remain as that and I won't have to turn it into:
<link rel="stylesheet" type="text/css" href="/folder/folder/themes/style.css" />
I hope this all makes sense.
EDIT:
Hopefuly this explanation of my reasons helps a bit more...
1.- I want the final developer to be able to create themes as intuitively
as possible. So, the URLs remain as simple and intuitive as possible.
2.- I need to include the active theme into the root
directory, so it autoloads when the root is opened.
So if you combne, my reason number one, with my reason number two, then you
might understand how important it is for URLs to remain local and easy to
understand.
Instead of style.css you could request style.php, a file you define that uses imports root/folder/folder/themes/style.css and echoes it outright.
you can give a base tag in your code and point it to: the folder where you have the css files.
http://www.w3schools.com/tags/tag_base.asp
In root .htaccess file, add next lines:
RewriteEngine On
RewriteRule ^style.css$ /folder/folder/themes/style.css [L]
To include the theme file is simple:
include('root/folder/folder/themes/themeindex.php');
The other issue is specific to HTML. HTML needs to have the web path (not the absolute path) to the file so it can load it and use it.
The best you can probably do is something like:
<link
rel="stylesheet"
type="text/css"
href="<?php echo $theme_dir; ?>style.css"
/>
but that will still expose your theme directory. Why are you trying to hide that directory?
Response to comment:
If you take the code I have above and let your developer know how to structure their own content directory. All you would need to do it keep track of something like $active_theme_root and echo that out to load their own customized theme. As far as I know, you cannot set an HTML include directory, so HTML will need to know the web path to all needed assets
Best solution may be to have them use a global stylesheets folder for all themes ("root/stylesheets"). They would then link to this global folder in a relative manor:
<link rel="stylesheet" type="text/css" href="stylesheets/mytheme/style.css" />
File structure is as follows:
index.php
settings/
|-manage_account.php
templates/viriditio-v2/
|-index.tpl
templates/virditio-v2/css
|-style.css
localhost/~braden/virditio/index.php shows the template like expected showing index.tpl with the style sheet paths correctly showing:
<link rel="stylesheet" href="templates/virditio-v2/css/style.css" type="text/css"/>
However localhost/~braden/virditio/settings/manage_account.php shows the same path, which is the relative path (should be ../ to be complete).
What's an easy way to make it relative to the template? Or absolute to the root? Is there a Smarty function that includes css files and makes them not relative?
Currently I have it set like this:
config:
template_url = "templates/virditio-v2/"
and .tpl:
<link rel="stylesheet" href="{#template_url#}css/reset.css" type="text/css"/>
EDIT
It's not pretty but I was able to accomplish it with
{assign var='config_url' value=#template_url#}
{assign var='template_url' value=http://`$smarty.server.SERVER_NAME`$config_url}
Any better solutions out there?
Why not access the CSS like
/templates/virditio-v2/css/style.css
with an absolute path?
If its about different hosting environments in subdirectorys, consider a config option to set the base directory and append it as a vairable to the path in your smarty template.
If I understood correctly the issue is to have a CSS file linked to a webpage no matter where in the folder tree the page comes from. This might be written as:
<link rel="stylesheet" href="/~braden/virditio/templates/virditio-v2/css/style.css" type="text/css"/>
This would make it a fixed path starting from the root of the server.