Relative path in a php site on a windows server - php

I have a PHP enabled windows server, where i have to get a site working.
Its all well and good, the php is working, afaik. But for some reason i can not get relative paths to work in the page header.
I have this folder structure(only relevant folders):
wwwroot/Sitename/
wwwroot/Sitename/Control
wwwroot/Sitename/Images
wwwroot/Sitename/Model
wwwroot/Sitename/View
In IIS i have set the the physical path to wwwroot/Sitename/Control. This loads pages.php(which is located here) which in turn loads wwwroot/Sitename/view/header.php and footer.php.
This works perfectly, however in header.php there are some references to some javascript (and other) files located in wwwroot/Sitename/Model eg.:wwwroot/Sitename/model/js.js
(eg:
<link rel="stylesheet" type="text/css" href="../model/main.css" />
)
This file is not found by the browser when loaded. I am sure it has something to do with the fact that i have set the physical path as i have but this is needed to load pages.php.
Can anyone point me in the right direction? either why the relative path is not working or perhaps another way to set the physical path/something similar.
EDIT: clarifications in parenthesis

use ../ to go down a folder.
Try this in your header.php:
../Model

Why dont you use Absolute urls?
$SiteURL = 'http://www.your_iis_sever.com';
Then in your javascript files use
<?php echo $SiteURL;?>/path/to/js/jscript.js
infront of the links. Make sure its all correct => Check the source code when viewing the page if the paths are correct. From there its easier to navigate.

Related

Linking CSS file with PHP throgh all folders

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

Moving website to localhost: Document Root changed

I have download a website that was written from scratch using PHP, MySql, html, css, jQuery.
After fixing some configuration with MySql and htaccess, I get to a point where all my CSS and JS files are not loaded because of a relative path error.
This is how it looks:
The error in the console GET http://localhost/fonts/fonts.css sends me to index.php which loads all those files with a relative path.
Now, if I remove the / at the beginning of each line, the files will be loaded, however, I get more errors on different pages with bad URLs and such, that means that I need to fix this / problem instead of just renaming the hrefs.
So my question is, how to fix it?
I have tried to set a base href tag:
<base href="http://localhost/israelrescue/">
or
<base href="http://localhost/israelrescue">
or in PHP:
$_SERVER['DOCUMENT_ROOT'] = 'http://localhost/israelrescue';
or
$_SERVER['DOCUMENT_ROOT'] = $_SERVER['DOCUMENT_ROOT'] . '/israelrescue/';
P.S:
israelrescue is the name of the folder I am using in my localhost installation for this website.
Nothing seems to work, any ideas?
Thanks!
Answers:
It's because you've got the site in a subfolder in localhost. Is it possible for you to move the site into the root? I think #MukeshPanchal is telling you to load the assets from live, which is missing the point somewhat. – thebluefox
Looks like this site was not written to be run from within a
sub-folder. Easiest way to work around that would be to set up a
VirtualHost for this project in your local server, so that you can
load it directly via a local domain name, without any additional path.
(And btw., setting DOCUMENT_ROOT to an HTTP URL is complete nonsense.)
– CBroe
You can leave the site in its subfolder as long as you drop the "/" in the beggining of every file path :
<link rel="stylesheet" href="/css/main.css">
<script type="text/javascript" src="/js/common.js"></script>
becomes
<link rel="stylesheet" href="css/main.css">
<script type="text/javascript" src="js/common.js"></script>
Should work like this

PHP - Relative path to folder?

I am using an MVC codeigniter for a project I am working on. It is running on localhost just find and has a defined base_url='http://localhost/app/.
When running on the local machine, everything is working fine as expected and the images etc have a path that looks like: <link rel="stylesheet" href="http://localhost/app/assets/css/bootstrap.min.css">
The issue I am facing is when I use a VPN on my phone to look at the site. When on my local network, I type in the IP address of the server to load the website. This then causes images / css etc to still try and load from http://localhost and thus does not load them.
Is there a PHP variable that I can use that will get me the path to the folder the files are in? ie. ../../assets/bootstrap.min.css?
I tried using things like $_SERVER["DOCUMENT_ROOT"] but this gives me the full path /var/www/html/app/assets/css/ which again, isn't valid when I try to load this from another source such as my VPN.
How could I go about solving for this? Is this a path issue or something with the server I need to look into?
When you are wanting to collect files within the website you do not need to specify the url.
So if you have a domain whether it be localhost or a fqdn it doesnt matter and makes websites "portable"
Your links/images/script uri's can all be called relatively.
<a href="relative/path/to/file.html">
<img src="path/to/image.jpg">
<link rel="stylesheet" href="path/to/file.css">
<script src="path/to/file.css">
Doing this will overcome the portability issue, you will do the same in php some of the functions require the absolute path and those are documented but many allow you to use relative paths if you need a file from an adjacent directory you would use ../path/to/file etc.
Defining the path constant.
define('ROOTPATH', '/path/to/webroot');
From here I can combine my constant with the relative path wherever needed:
ROOTPATH . '/path/to/file'
I too faced the same issue. There seems to be a problem in the slash between views and html. Instead of back slash it contains forward slash. Work around: go to index.php and stop error logging by defining -1 to 0 as given below according to the environment.
Time being solution/work around:
if (defined('ENVIRONMENT'))
{
switch (ENVIRONMENT)
{
case 'development':
error_reporting(0);
Error message:
Warning: include(D:\xampp\htdocs\IPT\application\views/errors\html\error_php.php)

Change working directory in PHP doesn't affect HTML src

I have a php script in a folder "root/test/index.php" which calls another php script located in "root/app/run.php"
In the run.php file I change the php's working directory with a call to chdir to simplify my require_once paths
All the php includes work fine, however HTML paths are totally messed up.
To make my style.css works I have to use a path relative to "root/test/" even if my style.css is in "root/app/".
This works:
<link rel="stylesheet" type="text/css" href="../app/style.css">
and that goes on for all HTML sources like img etc.
How can I change HTML working directory too?
I looked in php/HTML documentation, but I have find no good solution.
Any clue?
Thanks in advance.
HTML don't know anything about the working directory of your PHP script.
The HTML is sent to the client who can't know what was the working directory.
You could add some redirection in an .htaccess file (if you are using apache) to redirect root/test/ to root/app or something like that.
Here is how it works :
You browser ask for /root/test/index.php
Your server handle all his things and then output HTML file
Your browser parse the HTML file
If it find something like that it will search the image here : /root/test/images/cat.png because you are viewing the /root/test/index.php file and you provide a relative path
Add a comment if you have more questions.

Hosted Site Not using CSS/JQuery

I have created a site, running it locally using XAMPP as i developed it, and it runs perfectly when local.
However i tested it when hosted and the CSS & JQuery (Google library) do not work at all.
The CSS tags use the standard:
<link rel="stylesheet" type="text/css" href="/css/css.css">
Is there a reason this wouldnt work when live? But surely this has nothing to do with why JQuery doesnt work either?
I dont feel comfortable leaving it live when it doesnt work, so im sorry i cannot give you a live version to view.
Any help would be great!
Do check with Inspector if the CSS and JS files are loaded correctly when you go live — do note that the first slash in front of the URL means you're referencing the root URL of the live site. Are the directory structures of the local and live site different?
Also, you can try loading the external files using the absolute URL, like http://domain.tld/css/css.css (double check if the file exists, or loadable). If it works, then it's your relative URL path that is not working.
[Edit]: For the sake of completeness, this is how relative URL paths work:
/<fileName>.<fileExtension> will point to the file at the root directory
./<fileName>.<fileExtension> will point to the file in the same directory as the current page
../<fileName>.<fileExtension> will point to the file in the directory one level above, relative to the current directory
Check that you have actually uploded CSS files into /css/ folder. (Obviously /css/ folder should be present on your server)
Make sure you can access you CSS files from the web: eg if you go herehttp://yourdomain.com/css/css.ss you should see the contents of you CSS file.
if you can see the contents of your file here: http://yourdomain.com/css/css.css
then try this:
<link rel="stylesheet" type="text/css" href="http://yourdomain.com/css/css.css">
And as #Terry already mentioned check the paths.

Categories