For some reason this path works perfectly on one developer's machine
include dirname(__FILE__) . '/../includes/init.php';
But on my machine I have to remove the '/..' section for it to work.
include dirname(__FILE__) . '/includes/init.php';
I suspect this is linked other issues with the application on my machine.
Why is this occurring and what can I change to get it working like it does on the original developer's machine?
When I encountered something similar, I used the getcwd() function in PHP to see that the working directory is different.
I managed to solve it by chdir to the current directory at start, and the relative paths have been working ever since.
Or you can use absolute paths.
http://php.net/manual/en/function.chdir.php
You could check the DOCUMENT_ROOT on each of the dev machines. Ensuring you're working from the same directory etc.
<?php
$path = $_SERVER['DOCUMENT_ROOT'];
echo $path;
?>
Related
I am attempting to move an old Intranet site running on Apache 2.2, to a WAMP setup (latest version) on my local machine.
One of the issues I have currently, is a require_once path failing to open, and I cannot determine what the cause is.
I have WAMP installed in:
C:\Wamp
I have changed http.conf and vhosts.conf to change the document root from
C:\Wamp\www
to
C:\Wamp\www\Intranet
This folder contains an index.php, which I can see being loaded correctly when browsing to localhost.
Index.php has an iFrame that loads welcome.php from
/site/welcome.php
This works, as the iFrame loads, but throws a 500 error.
Enabling PHP errors, the welcome.php page in the iFrame gives me an error on a require_once. The require_once is:
/site/login/config.php.
As you can see, I am using absolute paths here, so the fact that index.php is able to load /site/welcome.php, tells me it is loading the correct file from:
C:\Wamp\www\Intranet\site\welcome.php
I would expect then, my require_once with an absolute path to be loading:
C:\Wamp\www\Intranet\site\login\config.php
Which is a valid file path.
What is confusing me, is that the first absolute path I am using, seems to be starting from the document root, not the physical directory root.
The second absolute path I am using, does not seem to be starting from the document root.
Even more interestingly, if I change the require_once from:
/site/login/config.php
to
/login/config.php
It works?! I wouldn't expect it to, as that would suggest the absolute path I am specifying, is in fact a relative path?
I don't think this path (/site/login/config.php) makes sense on a windows machine.
Better than hardcoding the full file path, determine it on runtime.
E.g.:
define('ROOT_DIR', realpath(dirname(__FILE__)));
// because *nix and Windows path separators aren't the same (/ vs \)
define('DS', DIRECTORY_SEPARATOR);
// just for convenience sake, you could use DIRECTORY_SEPARATOR on its own.
Then including your config file, from welcome.php would be:
require_once(ROOT_DIR . DS . 'login' . DS . 'config.php');
I think that besides you are confusing the path that you use in the iframe declaration (which is a url path) with the path you use in the require (which is a filesystem path).
How i tend to resolve most of my include/require errors is by setting a variable path:
$path = dirname(__FILE__);
include($path.'/file.php');
This will prevent you from having trouble with different environments in this case being windows and linux.
Or even better:
define('ROOTPATH', realpath(dirname(__FILE__) . '/'));
require ROOTPATH.'login/config.php';
Another thing, probably not causing your issues, but being good practice is to set the DIRECTORY_SEPaRATOR.
define('DS', DIRECTORY_SEPARATOR);
include(DS.'home'.DS.'www'.DS);
This will make PHP use the correct slash (either / or \)
< Referring to Make path accessible at various level of folder on PHP >
The code below:
require_once __DIR__ . '/../FolderB2/PageB21.php';
This works locally, but not when I have uploaded the page to the server. Is my hosting company restricting access to this?
If you run this code, do you get the exact path you are expecting?
echo dirname(__FILE__) . '/../FolderB2/PageB21.php';
Perhaps the relative path is wrong or maybe there is a mistake in the casing (i.e. if your local machine is windows, paths are not case sensitive, but they would be on a Linux server).
Perhaps you can use $_SERVER['DOCUMENT_ROOT'] . '/path/to/file.php' if your host runs PHP less than 5.3
I'm trying to include a file v-functions.php that's in htdocs/sc-dev/
The file I'm working on is in htdocs/sc-dev/accounts/verify
How do I write the include for the file v-functions so I can include it in index.php which is in /verify
I tried using include ('../../sc-dev/v-functions.php'); but that wont work, although it works fine in /accounts.
I'm running php 5.3.13 on Windows 7 Home Premium using apache server
You could always use an absolute path, since you know it:
include("/htdocs/sc-dev/accounts/verify");
Alternatively, you can make use of the $_SERVER['DOCUMENT_ROOT'] superglobal, which contains the base path of the website.
In mac os, it worked with:
include('../../v-functions.php');
You can just use realpath and step by step check which location you're getting with each "../".
Anyway path to the file you want:
htdocs/sc-dev/v-functions.php
Your current path:
htdocs/sc-dev/accounts/verify/somefile.php
Leaving once ("../"):
htdocs/sc-dev/accounts/
Leaving twice ("../../"):
htdocs/sc-dev/
Picking the file ("../../v-functions.php"):
htdocs/sc-dev/v-functions.php
I hope it will help you understand.
You can use absolute path. Use $path = $_SERVER['DOCUMENT_ROOT']; to find out what your root path is then set the directory path.
I'm having some problems referencing a file on my website in a PHP script. Let's say the full path is
www.mydomain.com/img/peggy.jpg
I can't use relative addressing like in
../img/peggy.jpg
because the script is in an include file which is included in several files at different levels.
/img/peggy.jpg
doesn't seem to work either. How do I reference to an absolute path?
TIA
Steven
You could define(BASE_PATH, dirname(__FILE__)) in your index.php.
Then use $fileName = BASE_PATH . "/img/peggy.jpg";
You would have to use dirname(__FILE__) . "/img/peggy.jpg".
You can use multiple, as well: dirname(dirname(__FILE__)) ...
$_SERVER['DOCUMENT_ROOT'] points to the absolute path of the current web root.
Solved by writing the full path explicitly:
/home2/mydomain/public_html/img/peggy.jpg
Note to self: will have to edit this if I ever move to another hosting service. Even my current hosting service (Bluehost) may one day decide to rearrange its file structure.
Other solutions are still welcome!
I'm doing some PHP stuff on an Ubuntu server.
The path I'm working in is /mnt/dev-windows-data/Staging/mbiek/test_list but the PHP call getcwd() is returning /mnt/dev-windows/Staging/mbiek/test_list (notice how it's dev-windows instead of dev-windows-data).
There aren't any symbolic links anywhere.
Are there any other causes for getcwd() returning a different path from a local pwd call?
Edit
I figured it out. The DOCUMENT_ROOT in PHP is set to /mnt/dev-windows which throws everything off.
Which file are you calling the getcwd() in and is that file is included into the one you are running (e.g. running index.php, including startup.php which contains gwtcwd()).
Is the file you are running in /dev-windows/ or /dev-windows-data/? It works on the file you are actually running.
Here's an example of my current project:
index.php
<?php
require_once('./includes/construct.php');
//snip
?>
includes/construct.php
<?php
//snip
(!defined('DIR')) ? define('DIR', getcwd()) : NULL;
require_once(DIR . '/includes/functions.php');
//snip
?>
#Ross
I figured it out and updated the OP with the solution.
#Ross
I thought that getcwd() was returning a filesystem path rather than a relative url path.
Either way, the fact remains that the path /mnt/dev-windows doesn't exist while /mnt/dev-windows-data does.
#Mark
Well that's just plain weird! What's your include_path - that could be messing thigns around. I've personally ditched it in favour of contants as it's just so temperamental (or I've never learned how to do it justice).