defining root directory for CSS & images in php - php

This is my code below, i need to set a root directory for css files to access file from anywhere in application. But this is not working for getting images & css files.
define('ROOT', __DIR__);
define('RCSS', ROOT.'\css');
define('RIMAGES', ROOT.'\images');
And my Html Page Content is..
<link rel="stylesheet" type="text/css" href="<?php echo RCSS.'\master.css'; ?>"

You are doing it in a wrong way. Try to use "http://" . $_SERVER['SERVER_NAME'] instead of __DIR__. Because you should pass web path to your html files.

Related

Loaded styles and css but not displaying properly

So I tried to import a web HTML CSS template into my self-made MVC.
When I open the template itself it works properly. But when I access it through my controller it can not connect to CSS files and does not display proper style. Only blank HTML without any styles.
This is how I am trying to access it
class NewsController {
public function actionIndex() {
$newsList = array();
$newsList = News::getNewsList();
require_once 'views/index.php';
return true;
}
This is code from HTML head where it links the CSS
<link rel="stylesheet" href="css/animate.css" >
<link rel="stylesheet" href="css/icomoon.css">
<link rel="stylesheet" href="css/bootstrap.css">
<link rel="stylesheet" href="css/magnific-popup.css">
<link rel="stylesheet" href="css/style.css">
this is how folders are located(highlited the controller, and views folders is a full template ) folders tree:
EDIT
I checked with inspec all the elements are loaded but do not display any styles. Whats the matter?
You need to add './' at start of CSS paths. This allow browser to use absolute file path.
./ is has nothing to do with an absolute file path. It just marks the current direcory.
If you want to use the absolute filepath, do it with php or the equivalent in your template language:
<link rel="stylesheet" href="<?php echo __ DIR __ . DIRECTORY_SEPARATOR . 'blogv4' . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'css' . DIRECTORY_SEPARATOR . 'animate.css'; ?>" />
?>
EDIT: Due to the image of your folder tree you can write:
<link rel="stylesheet" href="blogv4/views/css/animate.css" />
You need to enter the path from your bootstrap file (e. g. index.php), which contains your header.
Make sure the PHP script (in your case, the index.php handling the request) that's calling your Controllers lives in your document root where your assets are.
And, if I am the one, I'd write an helper function for handling my assets, like
<?php
function asset($resource) {
$link = preg_replace('/\//', DIRECTORY_SEPARATOR, $resource);
return $link;
}
?>
Like that, you can render the absolute path of where your assets are by adding the protocol and domain prefix in front on the return value.

Page Template PHP file not linking CSS file

I have the php file and in that I am linking a HTML file.
include 'practice.html';
In the HTML file I have a few CSS files linked.
<link href="practice1.css" rel="stylesheet" />
<link href="practice2.css" rel="stylesheet" />
Now, what is happening is the Page Template is displaying only the HTML part and not able to access the CSS files.
How can I possibly use CSS files?
PS : ALL my PHP, HTML, and CSS files are under the same folder (wordpress\wp-content\themes\*).
On your PHP template page, use:
<?php require(TEMPLATEPATH.'\practice.html'); ?>
And on your HTML page, use:
<link href="\wordpress\wp-content\themes\YourThemeName\practice1.css" rel="stylesheet">
<link href="\wordpress\wp-content\themes\YourThemeName\practice2.css" rel="stylesheet">
Make sure to replace YourThemeName with, your theme's name. And also to provide the correct path of where your CSS files are located.
If your wondering about what TEMPLATEPATH does, it provides the path to the template in use by WordPress so you do not have to type out the full path ex. \wordpress\wp-content\themes\YourThemeName\
Are you working on a local server or a live website? Depending on which one it is, you will have to change up the paths.
Example:
For a live website use:
/wordpress/wp-content/themes/YourThemeName/
or
For a local server use:
\wordpress\wp-content\themes\YourThemeName\
try using Include PHP function for including HTML file and for the CSS you can simply use echo
<?php
echo '<link href="practice1.css" rel="stylesheet">';
include('practice.html');
?>

trouble with php include and file paths

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" />

php include file from another directory

Here is a structure example:
/main
/css
style.css
/include
article1.php
article2.php
header.php
index.php
In my header.php I have the following code for the css:
<link rel="stylesheet" type="text/css" href="css/style.css" />
And, for example, in my index.php I have the following as the first line:
<?php include 'header.php'; ?>
Now, everything works fine as it is. Next I'm going to insert the following code in the article1.php file:
<?php include '../header.php'; ?>
The contents (menus and other html) are displayed correctly, however the CSS won't be displayed/recognized at all. Basically what's happening is that the header file is being included but the server isn't respecting the directory parenting. For the CSS to be displayed correctly I'd have to change the link rel for the CSS to ../css/style.css, but if I do so it won't work on files located in the main directory.
I hope I made my problem clear. What am I doing wrong? How can I include files from different directories and preserve the links inside them?
In your site's <head> section, insert a <base> element with the href attribute. Set the href attribute to the base URL of your website, and all relative requests will be sent through that base URL.
<base href="http://my-website-url.com/" />
<link rel="stylesheet" type="text/css" href="css/style.css" />
With a correctly-set base tag, the user's browser will attempt to load your stylesheet from the URL http://my-website-url.com/css/style.css.
Note: this not only affects stylesheets, but all relative links in the document.
It has to do with how pathing works in includes. I recommend pathing things from the doc root whenever possible.
<?php include( $_SERVER['DOCUMENT_ROOT'] . '/header.php' ); ?>
That will work in any of your files.
Instead of using relative paths:
href="css/style.css"
use absolute paths from the webroot:
href="/css/style.css"
You should include your css file from the root. So /css/style.css so way it will always start at the root and then go down from there. I believe that should fix your problem in all cases.
First off, the problem is that your link to your CSS files is wrong. The best thing to do is look at the output of the HTML (view source) from the page. So lets break it down (form index.php):
Index.php is located at domain.tld/index.php. Your css files are located at domain.tld/css/*. When viewing the index file, your header is
<link rel="stylesheet" type="text/css" href="css/style.css" />
which works because you are at the top of the directory (or root) and the links are made relative to this directory. Now when you go up to domain.tld/include/article1.php, you are no longer at the root. It is trying to access:
domain.tld/include/css/style.css
This means you have to build the full link or change your relative. The easy way since the CSS is at the root is just to use the following
<link rel="stylesheet" type="text/css" href="/css/style.css" />
^
This means it will look at the root of the domain for the css directory as such domain.tld/css/styles.css. If your top directory is /main, use /main/css/styles.css
Using relative includes from filed already called as includes is always a problem, each time you specify a relative location, replace your
../some_directory/some_file.php
with
dirname(dirname(__FILE__)) . '/some_directory/some_file.php'
dirname(__FILE__) is the current directory.
May I explain your problem and how to solve it.
An include in php works as in C.
When you include a page that copy/paste content of the page into the first one.
So, there are two files:
include.php
<?php
$var = 'PHP';
?>
main.php
<?php
include 'include.php';
echo $var; // affiche 'PHP'
?>
When you request for "main.php" page you will get the following:
<?php
$var = 'PHP';
echo $var; // affiche 'PHP'
?>
So if you need to include an element to your page. You have two choices:
1. absolute path (better way)
Instead of using relative path, use absolute path. This way allows you to include a file from everywhere without of use of the current path.
2. variable
You can use a variable containing the relative path to the root directory of your repository/website. So each time you have to include a page or create one you have to define a variable as following:
include.php
<?php
$var = 'PHP';
echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"".$currentPath."css/style.css\" />";
?>
main.php
<?php
# path to the root of the website
$currentPath = "../";
include 'include.php';
echo $var; // affiche 'PHP'
?>
To learn more about include, see this page
You have two options
Pass the relative path to the included file
ie.
<link rel="stylesheet" type="text/css" href="<?=$path ?>css/style.css" />
and
$path = "../";
include "header.php";
or 2. you use absolute paths to your files
<link rel="stylesheet" type="text/css" href="/css/style.css" />

URL rewrite with include does not load stylesheet

All my files are in the public_html folder. I rewrote the url of the pages with .htaccess, so e.g the url of mywebsite.com/balance.php looks mywebsite.com/myaccount/balance.
I can include a file with: <?php include 'header_login.php'; ?>, but it appears without the stylesheet.
Both this .php file and the .css file are in the public_html folder.
If I rewrite the url to mywebsite.com/balance it works.
How can I make this work with this "virtual" folder in the url?
Simple.
Always use absolute paths in you HTML and CSS files.
An absolute path always starting from / pointing to the web-server root.
So, make your css path like
/css/styles.css
or whatever.
The address of the stylesheet is possibly not correct. You have to use absolute paths.
If your structure is like this:
/balance.php
/style.css
In your balance.php you use: <link rel="stylesheet" href="style.css">
And rewrite it to: /myaccount/balance
The browser will look for a style.css file at /myaccount/balance/style.css .
Just change it to an absolute path and you will be fine.
You can define your base url
define ("BASE_URL","http://www.mysite.com");
and add BASE_URL with stylesheet like
<link rel="stylesheet" type="text/css" href="<?php echo BASE_URL; ?>/style.css">

Categories