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
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');
?>
INTRO
I am new to php. I love that it allows me to change one header.php file and it updates all over the site.
IF all - index.php, header.php, style.css, article.php, homework.php files are in the ROOT folder, everything works like magic, I like it. I use:
<?php include_once "header.php"; ?>
at the top of index.php, article.php and homework.php and the header appears.
to load css a regular =
<link href="style.css" rel="stylesheet" type="text/css">
is enough to have in my header.php file, because all the files are in the same directory.
MY PROBLEM
When the amount of articles becomes too large and I decide I want to put those articles in different folders, that is when stuff gets confusing and I would like to know a proper way to solve it.
New website folder structure
C:\xampp\htdocs\articles\homework.php
C:\xampp\htdocs\views\header.php and style.css
C:\xampp\htdocs\index.php
C:\xampp\htdocs\articles.php
Now please help me how to make homework.php file to load the css from the header.php? I manage to load the header itself with
<?php include_once "..\views\header.php";?>
BUT the css file doesn't load for some reason.
I read something about "basenames", "site roots", but don't know how to properly set them up.
The perfect scenario
The perfect scenario would be if I could have a basename variable that I can change, so when I make my server live I can just change the basename to the appropriate new server directory and because all the header.php and other blog files were linked to that basename, everything would change automatically. I have done too many manual directory rewriting to do it once again, please tell me a way to automate it :)
Thank you a lot!
p.s!!!!!! Before I even post this question I realized that the header.php is trying to load views/style.css, which doesn't make sense, because the style.css file is in the same folder as header.php now.. Somehow basenames, site roots are a must here I believe...
You can specify relative paths such as ../css - means up one folder then look in css folder or ../../ - means up 2 levels then look in css folder.
../../main/css/style.css - would mean up 2 levels then look in main/css for the file style.css.
The problem with using / or ../ etc is that if you decide to change the location of the resource you still have to change all your paths. Using a var you can simply change it to reflect the new location.
with PHP you can also getcwd() and dirname() and realpath() to get a string representing a location, you can then set a base variable for your files and 'path' down from it.
This way you can use the same variable to locate a file rather than different relative paths depending on the level of the file calling it.
DIRECTORY_SEPARATOR is also useful for avoiding errors between / and \ with linux and windows OS. However I believe Windows and Linux will both resolve /
Personally I like to set path locations to commonly used files such as /includes in config.php then I can use that setting from anywhere
In summary you are just either discovering a path using PHP or setting a path as a variable
$path = 'c:/htdocs/mysite/includes/';
then using the variable as part of the path name when you access the file
$_SERVER['DOCUMENT_ROOT']
can also be useful to identify your site home folder
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.
I have a folder that contains many different files:
folder1:
somthing.php
somthingelse.php
blah.php
ect ect....
Now, I don't want the files in that folder to access (include) files outside the folder. So it's okay to include('blah.php') but not okay to include('../blah.php')..
Any ideas on how I'd go about doing this?
This is only possible on the CLI and in a Unix environment. You would have to use chroot to hide other folders.
http://php.net/manual/en/function.chroot.php
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.