I am trying to create a root directory on my localhost(using xampp) so that I can link to images and folders found on the root from subfolders. So my directory is built like seen below.
Also, would this method work when the site is uploaded on the web? or would I have to change document root to http:// links to my site?
F = folder
f = file
My Site
-------
[f] index.html
[f] logo.html
[F] css -> [f] main.css
[F] news -> [F]1 -> [f] 1.html
[F] images -> [f]logo.jpg
logo.html will be included using PHP on every html page.
So if I include the logo.html in both index.html & news/1/1.html link to image doesn't work since the news file is on a different folder and link to the images folder changes.
I tried this using PHP but it seems I am doing it wrong:
<?php $root = $_SERVER['DOCUMENT_ROOT'].'/My Site/'; echo $root .'/images/logo.jpg';
?>
$_SERVER['DOCUMENT_ROOT'] gives you the real file-system path, not the path returned by Apache.
To link to the resource you need to use the HTTP path. If you want to link from the root you just need something like /images/logo.jpg
I found that on my PHPBB3.1 driven website using the "Pages" addin, the relative link would not remain 'static' in some link buttons at the top of my site.
From the PHPbb3 index page a link such as app.php/page/FT would produce an address of http://earth/forum/app.php/page/FT (Correct) ,but from the "Pages add in) that same link produced http://earth/forum/app.php/page/app.php/page/FT (incorrect).
I started looking for a way to set my page relative to the root of the web. I could NOT use absolute links because my site is replicated on other servers. I found the answer by trial and error. Simply add several ../../../ like this ../../../forum/app.php/page/FT
and it works.
Perhaps not the best of computer programming but it does the job.
In a nutshell with each ../ you step back a directory, put too many in and it goes back to the root but cannot go back any further!
Related
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.
This is the structure of my messy host root folder; which I'm hoping to clean up soon;
root (folder)
.....AllWebsites (folder)
.......Website1 (folder)
index.html//please note that these html files below are not in their own folder. So they're sitting in the website1 folder loosely;
index2.html
index3.html
numerous numerous other html files
phpFolder (folder)
..........index.php (file)//this is my landing page
website2 (folder)
and more folders for other websites
So I simply want a button on my index.php called "Second Landing Page" to link to index.html. So I tried writing;
<li><a href='/website1/index.html'><strong>Second Landing Page</strong></a></li>
But I get the 404 error. Then I tried writing;
href='/allWebsites/website1/index.html
href='//website1/index.html
//I was hoping 2 slashes would bump me up one level but didn't work and many other variations without any luck. This is probably a very simple question but not for me learning as I code these sites.
One way to fix this problem would be to just place a new folder in the phpFolder called htmlFolder and paste all the html files including index.html. Then I would simply write;
<li><a href='/htmlFolder/index.html'><strong>Second Landing Page</strong></a></li>
But this will be a nightmare, so I'd appreciate any help to simply figure out how to link to the index.html where it currently resides > in the folder above phpFolder named website1 folder.
The term you're looking for is "relative path".
Taken from http://www.boogiejack.com/server_paths.html
I have phpbb in a directory of my joomla install which also happens to be inside a directory on the root (so that's me.domain.com/joomla/phpbb). I have successfully been able to rewrite joomla's directory to the root of a parked domain (me.domain.com/joomla/ --> domain2.com/ ) but when every I go to domain2.com/forum non of the style sheets work but domain2.com/joomla/forum works fine. Any help please?
Edit
I found that the css files werent being called properly. The page was calling from domain2.com/styles/ instead of from domain2.com/forum/styles/
I need to be able to access a file in a folder located in the root directory on each of my pages.
Is there a piece of code I can use to type out the full directory going from the root folder to the specific folder I want and be able to copy onto each page without having to change the code?
For more clarification, it's hard to word, but I want to be able to link top my CSS file in the directory /css on each page without manually having to put ../../ etc.
Or does anyone else know the best way to link one style sheet to each page with ease?
If the root of your website has the path / (in other words, the address you type for the root of your website is something like http://mydomain.com/) then you can simply refer to your CSS files using /my.css or /css/my.css in all pages. The leading / tells the browser to look for these files relative to the root of your website.
You should be able to use a relative path of /folder path that will be accessible from all your pages.
You should also be able to do it the same was Cristian is recommending with:
$folder = $_SERVER['DOCUMENT_ROOT']."/folder path";
I'm using XAMPP on my home machine and also using it as a web server.
I have a main folder which is for my site, I then have a dev folder where I'm trying to work on a new site. (not sure if this is relevant info but worth mentioning)
On my local machine the site is c:/MySite/Dev
within Dev I have something along the lines of
Dev
assets
css
js
includes
footer
header
core
init
folder
subfolder
files.php
index.php
init is included at the top of every page, header and footer includes are also included. Within header is my nav bar.
I'm trying to define a site root so that I don't have to keep changing ../ and ../../ etc depending on the location of the files.
Also, seeing as my header gan be in many subfolders, the links to main index etc can get messy as index.php from a subfolder won't take me to the root index.php
I've tried using DIR and SERVER_ROOT bt of which I couldn't get to work properly, server root returned the c:/www and DIR returned the folder of the file in which it was called.
I'm still new to php so learning as I go.
This is what I've tried:
in init:
define("WEB", "http://mySite.com/Dev");
$environment = WEB;
echo $environment;
this works in the first instance, then when I'm in the subfolder and try using the home link I get :
http://mySite.com/Dev/Folder/Subfolder/%3Cbr%20/%3E%3Cb%3ENotice%3C/b%3E:%20%20Undefined%20variable:%20environment%20in%20%3Cb%3EC:/www/Dev/includes/header.php%3C/b%3E%20on%20line%20%3Cb%3E38%3C/b%3E%3Cbr%20/%3E/index.php
can anyone help please?
EDIT: The error after cleaning the output:
http://mySite.com/Dev/folder/subfolder/
Notice: Undefined variable: environment in C:/www/dev/includes/header.php on line 38
/index.php