problem referencing file in PHP - php

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!

Related

How to require a relative path in PHP

How can I PHP require a file using a relative path on a Linux server from inside a symbolic link?
I tried:
require_once(dirname(__DIR__) . "/app.config.php");
But that is throwing an error saying the file does not exist. Here is the file structure:
/srv/www/accounts/dev
app => /srv/www/myapp
index.php
app.config.php
So app is a symbolic link to /srv/www/myapp but when I try and require app.config.php php get's confused and tries to require /srv/www/myapp/app.config.php instead of /srv/www/accounts/dev/app.config.php.
How is this possible? Thanks.
Try require_once(__DIR__.'../app.config.php');
Hope that works :)
Just a note, if you're on PHP < 5.3, try dirname(__FILE__) instead of __DIR__ :)
require_once(__DIR__."/../app.config.php");
i think its the way the folders are setup, if u have like the /srv/www/accounts/dev/app.config.php. is in a different folder then /srv/www/myapp/app.config.php
ones in /accounts/dev and the other is in /myapp
you dont want a relative path you want absolute, relative would only be in that folder you have to at least let it know which folder to check, so i think you wanna go to the a different folder
You might try using realpath() to get the path. That is supposed to expand symbolic links and return the absolute path. Something like:
require_once(realpath('../') . "app.config.php");

PHP absolute path in requireonce

I'm using a simple pre-made authorisation tool to secure my site. The tool requires this line of code to be applied to the top of every .php page. Auth.php lives on the root level.
<?php ${(require_once('Auth.php'))}->protectme(); ?>
I need to be able to add this line of code to every file, including files in sub-folders. But I'm unsure of how to apply the method as shown in require_once in php to ensure it always is linked absolutely, alongside the protectme(); function.
Any enlightenment is appreciated.
Cheers.
Set a variable to the absolute path. $_SERVER['DOCUMENT_ROOT'] is a PHP global variable that stores the servers, root path. You just need to insert the rest of the path information to the script. For instance if your website exists on /var/www/ and your script exists at /var/www/scripts you would do the following.
$path = $_SERVER['DOCUMENT_ROOT'] . '/scripts/Auth.php';
require_once($path);
You can use a relative path to get to it.
One level up: ../Auth.php
Two levels up: ../../Auth.php
etc.
You should alter your php.ini to change the include path to the the path to that (all all your other) included files. Then you can include them without a path on every page regardless of their own location.
More Details
Add the root level directory to your include_path - PHP will do the rest for you. No complicated variables, constants or whatever.
In addition to everything that has been said already, I suggest centralizing all common functionality. Create a file, like common.php with all includes that you need for you application and then include that from all your scripts.
Also, a nice way to do relative includes is by using dirname() function in combination with __FILE__ magic constant. For example:
require_once dirname(__FILE__) . '/../lib/common.php';
If you do not have access to php.ini, I've used something like this before
include $_SERVER['DOCUMENT_ROOT']."/"."include_me.php";
The easiest way since it's a site wide change is to add its directory first in the include path in php.ini.
If you can't change php.ini, there are a few other options for adding it to the include path.

PHP Relative paths like ASP

This works
<?php include("inc/c.php")?>
But in a folder past this, this does not work
<?php include("../inc/c.php")?>
I have to do
<?php include("/var/web/public_html/etc/inc/c.php")?>
I know in ASP you can enable virtual paths and directories. Is this the same with PHP?
If you're including a file from a folder, all includes are relative to the includer's file.
Therefore, the same code should work for the file in the sub-folder:
<?php include("inc/c.php")?>
You can use realpath(dirname(__FILE__)) to include files relatively to current file:
include(realpath(dirname(__FILE__).'/../inc/c.php'));
You can add directories to PHP's include_path directory. When you specify a relative file name, PHP will look for that file relative to all directories specified in the include_path.
Take a look at http://php.net/manual/en/function.set-include-path.php#example-488.
use
dirname(__file__)
it will return you path of current directory.
But in a folder past this, this does not work
Nope, it does work.
I know in ASP you can enable virtual paths and directories. Is this the same with PHP?
Yes. But whole virtual path thing has nothing to do with your case.
This is not PHP problem. This is developer's problem who is using wrong path.
To make your code fool-proof, always use absolute paths. Build paths not from current location but from the site root. So, it will be all the same in the ANY page on your site.
Most general way would be
include $_SERVER['DOCUMENT_ROOT']."/etc/inc/c.php";

DOCUMENT_ROOT is not complete, missing domain folder

I have searched many threads so far but cant seem to find a solution. Inside one of my php scripts I am trying to get a server document root but the value I get is not complete, its simply missing the domain folder. I believe it is due to sharing hosting or smth else.
Here is the current way I am using:
$root = realpath($_SERVER["DOCUMENT_ROOT"]);
and the path I get is like:
/home/content/01/0151247/html
although I know it should be like:
/home/content/01/0151247/html/mydomain
I know as I compared it with SCRIPT_NAME and I see the mydomain there in the path.
Hope someone could direct me.
Thank you and sorry for probably asking another thousand time same question over community, I really tried things around from here, nothing helps me so far.
UPDATE
unfortunately I cant not simply use my index file with DIR as it is a wordpress setup and I am working on a separate folder where I am including some wordpress functionality but for that I need a document_root. If that would help.
UPDATE
apparantly the following way resolved my case, maybe it will help someone one day:
realpath($_SERVER["SUBDOMAIN_DOCUMENT_ROOT"]);
basically because of the server setup and domain configured as a subdomain.
Thanks to all who participated.
Prior to PHP 5.3 you can put a file in the directory whose path you want and define a constant:
define('ROOT_DIR', dirname( __FILE__ ));
After 5.3 you can just do:
define('ROOT_DIR', __DIR__);
The idea being that this would be in config.php of some sort that is included every time the application runs.
Magic Constants Docs
UPDATE
In the config file, you can just append the DOCUMENT_ROOT variable:
$_SERVER['DOCUMENT_ROOT'] = $_SERVER['DOCUMENT_ROOT'] . '/mydomain';
And that should take care of it for you.
Old Solution
The DOCUMENT_ROOT is an environment variable set by the server. So if this is on shared hosting, you cannot changed. An alternative is to set your own constant to it, so in a config type file that is included on your pages you can do something like:
define('DOCUMENT_ROOT', $_SERVER['DOCUMENT_ROOT'] . '/mydomain');
And then just use that constant in place of $_SERVER['DOCUMENT_ROOT']. The other option is to contact your host and inquire about it, maybe it was an oversight on their part and they will fix it.
EDIT
Probably using the __DIR__ as others have posted about is the better way, as the DOCUMENT_ROOT can be set to different items and at least with the __DIR__ you should get an accurate directory each time.
Personally, to get the root of a folder in PHP, I use this in the my index file:
define('ROOT', dirname(__FILE__)); // __DIR__ will work under PHP 5.3

use absolute or relative path?

in my config.php where i have all constants i set the PATH to a absolute path.
but this means that when i move my application folder i have to change this path.
i wondered if its better to set a relative path, in that way whenever i move my application between production and development folder, i dont have to change it.
how do you guys do when you move between folders?
The best way I've found is to do the following:
define("PATH", realpath(dirname(__FILE__)));
That gives you the directory of the current file. If you do this in your settings/bootstrap/init file, you'll have it available to your application, and it will work for any file system.
__FILE__ is your friend.
define('BASE_PATH', dirname(realpath(__FILE__)));
This will make your scripts more portable.
Include a file like this
include BASE_PATH . 'includes/header.php';
IMO, absolute paths are bad news. Even if you don't plan to move, your hosting provider could move you, like DreamHost recently did to me. I was fine....
But there are 14 references to "path" on their wiki:
http://wiki.dreamhost.com/Server_Moves
I do three things to solve this:
The first is to use paths relative to the current file and include things using dirname(__FILE__).
The second is to use a loader include that all the pages load. This file has one responsibility: to find the include directory, usually via a relative call. So long as this relative relationship stays, it doesn't need changing.
I also like to support custom settings that belong to the installation rather than the codebase. This is done by an include mechanism and overrides a few settings that will be specific for the server the code is on.

Categories