I have a PHP page on my site in a sub folder called Articles.
The page is article.php.
The article.php page requires a common php page called _head.php. This provides the header for the pages.
_head.php is located in the root directory.
The /Articles directory is a subdirectory within the root.
I've included this _head.php page in article.php this way:
<?php include("../_head.php"); ?>
And this works fine.
The problem, however, is that the image elements within _head.php are located in the 'images' subdirectory (also off the root) and are referenced relative to the _head.php being in the root, like this...
<img src="images/services.gif">
So if I use _head.php for files on the root, it works great and shows all the images correctly. But when I include _head.php into a php file that is not in the root, but instead in a subdirectory like /Articles (/Articles/articles.php), the images do not show up.
Do I need to change the _head.php file in how it references the images or is there some code I'm supposed to include in articles.php when including _head.php that tells it how to use _head.php?
I'm concerned about using all absolute paths because if I have to move this site to another server this is going to cause me issues.
Mentioning what I follow not going to the hierarchical complexity,
For any PHP file that is being imported into another PHP file in root simple include/require_once (<path>).
For any file below root accessing other file anywhere within the root I use include/require_once (../<path>).
For accessing files which are outside the root, I use the absolute path of that file.
Working on few php files what I have seen using absolute path is the best thing in two ways, a) you are free from remembering the paths of different files and b) if you are using CDN or if your files are on different servers then this is very helpful. Anyways opinions may vary, this is my personal view/choice.
Related
I have my website on my local server (localhost - XAMPP). My website is broken up into three parts: 1)Header 2)Body and 3)Footer/Jscripts.
The header.php calls other basic files for the header section of the webpage. It works fine when I used relative paths, for example: include_once('./_css/main.css'); to call my CSS files or include_once('./_inc/metadata.php); to call my metadata for my pages.
The problem arises when I use an absolute path:
include_once('http://localhost/mywebsite.com/_css/main.css'); the same for the other files.
Why is that? This all started because I want to create subfolders because I had a general page, but now that I'm breaking it down to subpages I need a folder to contain the subpages
I was trying to include the css files and all the other resource files to the subpages, but they break using the relative paths method (./_css/main.css) being pulled from the header file located in the root directory. On the subpages, I'm using include_once('../_inc/header.php') to include the initial header.php file.
I want to avoid having to duplicate all the resource files from the root directory and adding them in the subfolder with their respective relative paths. That's why I was trying to use the absolute path method.
Any insight or clue would be appreciate as to:
Fix the 'Warning: include_once(): http:// wrapper is disabled in the server configuration by allow_url_include=0' error when using the absolute path.
2)Sharing css files in subfolders.
Using an absolute path may be a bad idea, what if you move from localhost to a real server? You'd need to make massive changes. Changing the project structure so that you could avoid all of this might be better, or if you really want to keep things as they are, use a variable.
<?php
$include_path = "whatever/path"
include('../header.php');
?>
and in header.php
<?php
include($include_path.'/my_css.css');
?>
I am working on a webpage that is located in a different folder than the root folder. I'm trying to pull in the newsletter.css file, the header.php file, and the services.jpg file. The working page is located at Root/newsletters/newsletter-2020-01.php. The header.php file is located in the Root folder, the newsletter.css is located at Root/styles/newsletter.css and lastly, the services.jpg is located at Root/images/services.jpg. The working file (Root/newsletters/newsletter-2020-01.php) isn't pulling in any of the other files. I've looked for the answer on Stackoverflow and everything is saying to use ../file.ext but this isn't working. Any advice?
<link rel="stylesheet" type="text/css" href="../styles/newsletter.css">
</head>
<?php include("../header.php");?>
<div class="background_image" style="background-image:url(../images/services.jpg)"></div>
For server level files (pulling in PHP files etc. that are done on the server, not the browser).
Use: $_SERVER['DOCUMENT_ROOT'], it's a sure thing.
https://www.php.net/manual/en/reserved.variables.server.php
$_SERVER['DOCUMENT_ROOT'] it gives you the full path of your server to the root where your HTML etc. files are stored. From there you use the path to the exact file you want.
To see it in action: <?php echo $_SERVER['DOCUMENT_ROOT'] ?>
It will make more sense.
Ex. $_SERVER['DOCUMENT_ROOT'] . '/directory/myfile.php'
As for browser level files (files accessed by the browser, .css, .js)
Use an absolute path from the root, again, a sure thing.
Ex. <script language="javascript" src="/directory/scripts/myjavascript.js"></script>
Why?
Relative paths can be broken if the file moves around. It may be relative to FileA, but FileB may be in a different location, or FileA may move, or be included in another file.
Absolute paths are a sure thing!
Consider a physical directory structure ( on Windows ) such as:
C:\wwwroot\htdocs\example
|_css
|_scripts
|_images
|_galleries
|_icons
|_banners
|_gallery
In the above typically example would be set as the document root and a call to echo $_SERVER['DOCUMENT_ROOT'] would yield c:/wwwroot/htdocs/example etc
An Absolute or Root Relative path for css, images, scripts etc would begin with a leading slash. So, to access css files you might do:
<link rel='stylesheet' href='/css/theme.css' />
or for an images
<img src='/images/banners/logo.png' />
If you start the path with ../ you are instructing the browser to fetch the resource beginning 1 folder higher ( than the current directory ) and then follow the given path. Similarly if you use ../../ that means "go up 2 levels" and begin searching from that point. There is a place for this syntax but it can be confusing and very easy to get wrong.
If you begin the path for a resource with no leading slash ( such as images/example.png ) you are instructing the browser to fetch the resource from the same level OR a sub-folder. In the given example here images/example.png suggests the image is within a sub-folder of the given path so if you are accessing the page at https://localhost/gallery/beach/index.php it would mean that there needs to be a folder structure C:\wwwroot\htdocs\example\gallery\beach\images for that to work ( unless using advanced trickery with the server config )
When it comes to including additional PHP scripts you can do so a little differently. It is not essential and in most cases it can be preferable to have the files you wish to include outside of the document root. Using the same base structure from above and having a directory for commonly included files outside of document root would mean that using a browser you could not directly open files in the includes directory - so for example: https://localhost/includes/secrets.php would fail but that file could easily be included from within PHP.
To include other PHP files you can tweak the includes_path variable using set_include_path() and thus allow PHP to include files from outwith the site root.
c:\wwwroot\htdocs\includes
You could ensure that all PHP scripts ( such as db connections and other class files ) are easily included by setting the includes_path:
set_include_path( 'c:\wwwroot\htdocs\includes' );
and then, to include a file from that directory:
require 'db.php';
You can set this to use the full path instead or you can use the ../../ style syntax also.
Im creating my own website, but I am having some path problems.
My scripts will not load when I refer to the path.
I have simply created a folder called "js" under my repository and are trying the following code:
<script src="js/startup.js"></script>
But the script wont load.
I've also programmed some PHP scripts and used include, and it seems to work fine there.
Any tips?
Regards
When including resources for the browser, the path is relative to the web root but when including php files, it is relative to the current file.
/project
/webRoot
-index.php
/js
-startup.js
/lib
-some.php
given this structure, your script tag would work, even if included in some.php. In index.php, you would need
include(dirname(__FILE__).'/../lib/some.php');
I have a problem on linking files such as stylesheets, images, database connection file(db.php), script files and etc.. because they are located outside the file where they are included/linked.
For example assuming this is the location of the main file where everything will be called:
my website directory/admin/thefilewhereeverythingwillbecalled.php
in thefilewhereeverythingwillbecalled.php, I must call the db.php which is located outside the folder that contains thefilewhereeverythingwillbecalled.php
e.g. my website directory/-this is the directory where the db.php is located-/thefilewhereeverythingwillbecalled.php - ditto the style sheets and script.
However the stylesheets and script are in the folder cssandjs (contains all stylesheets and script files) which this folder are located before the location of the thefilewhereeverythingwillbecalled.php
e.g. my website directory/-here is where the cssandjs folder is located-/thefilewhereeverythingwillbecalled.php
Generally I'm just having a problem on linking files which is outside from the file where it called those files. Can someone give me an idea how to link it please?
I tried this:
../cssandjs/style.cssand ./cssandjs/jquery.js but none of them work
If I'm correct, it's 2 directories up. Try this :
../../cssandjs/style.css
This will work if /cssandjs/ is in your website directory.
You should be a bit more specific with your directory names instead of -here is where this is located-
Can't seem to figure out which are in the same folder.
try to include full path to the files.
Try referencing the files like including the directories and files with
$_SERVER['DOCUMENT_ROOT']."/dirName/file.ext";
or use relative paths "../dirnName/file.ext";
first method is preferred
From my previous experience, I've almost always had problems with linking files with my website projects.
For example, linking CSS styles, Javascript files and including files in PHP. The problem is, that on my PC, the directory of my project was /www/project-name/ and when I put the project on a server, the directory would be just /www/. When I uploaded the project to a server, images wouldn't show, styles wouldn't work, database connections wasn't set, functions were not defined etc...
So my question is: What is the best and most efficient way to link/include files?
Something that will work no matter what the directory of the project is, and possibly, if I include project/includes/mysql.class.php in file1.php, and I move that file to a different directory, it would still properly include project/includes/mysql.class.php
You should use relative paths.
Instead of specifying the full path ('/www/project-name/includes/whatever.php'), use a path relative to the current location:
'./includes/whatever.php'
you can define the document root directory of project and then, include all files depending on it
put
define(DOC_ROOT, realpath(direname(__FILE__));
in your front controller, and when you have to include a file
include(DOC_ROOT . "/includes/file.php");
all frameworks uses this method
I'd suggest using a relative path (eg ../style.css or ../../style.css)
The ../ references the parent directory to the current file.
This is what I do, in general.
I use root relative urls inside html (e.g. src="/images/logo.jpg"). This way I can just copy the html from one page and past it in another without having to worry about the link not working becase the other page is inside a folder.
I relative urls in css, because all the resources I use inside the css, like images, I keep in the same folder as the css file (or a sub-directory of it). I mostly do this because it is shorter (url(img/background.jpg); vs. url(/css/img/background.jpg);). Minor added bonus is you could just copy the css folder to create a new theme based on the old one, without having to change all the urls in the css.
In PHP I use include($_SERVER['DOCUMENT_ROOT'] . '/includes/mysql.php');. You can just copy past the code into another file in another folder and it will still work.
The only time I rarely need to hardcode paths is inside htaccess.