How to require a relative path in PHP - 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");

Related

Php 5.4 Include / require -- cannot get file path right

Suppose i have this file:
/Home/user/docs/somewhere/inHere.php
And in this php, i want to require this:
/Home/user/other/well/buried/place.php
I know the difference between an absolute and relative path, but cannot seem to figure out how php wants this to look, i keep getting 'file not found or does not exist'
I am on a hostgator shared web server, if that has any bearing on anything.
Include it using absolute paths.
Either:
include '/Home/user/other/well/buried/place.php';
or do it relative from where you are, but still absolute:
include __DIR__ . '/../../other/well/buried/place.php';
The magic constant __DIR__ contains the absolute path to the file it was written in.
If you just do a relative path include '../../and-so-on', the starting point will change if you're including that file in some other file that resides in some other location.
Firstly you can get the current folder using getcwd().
Next, you can use $path = '../../etc'; $realPath = realpath($path). It will return false if the path is wrong, and the concrete path without relative ../'s if it is indeed an actual path.
If you still can't get it, var_dump($path); and then copy the path and try to cd into it, you should diascover what you are doing wrong at that point.
From the file location (somewhere) you have to go upwards (to docs) and another time (to user), and then go inside "other", like this:
include ("../../other/well/buried/place.php");
There you go.

PHP get directory of included file on Virtualhost

I wanna use __DIR__ on my apache virtual server, but it uses system adress (C:\xampp\htdocs\virtual\file.php), is there any way to use server's adress (/file.php or http://virtual/file.php)?
Thank you for your help
EDIT:
Thank you for your answers, but I wanna to include file.php elsewhere, so I can't use $_SERVER['REQUEST_URI']. Is there another way?
__DIR__ will give you the filesystem path of the folder of the file it is used in. Filesystem location and URL can be, depending on your HTTP server configuration, completely unrelated.
You may use $_SERVER['REQUEST_URI'] if you want info about the requested address by the client.
you have to use
$_SERVER['REQUEST_URI']
not
__DIR__
check this link enter link description here
__DIR__ The directory of the file. If used inside an include, the directory of the included file is returned. This is equivalent to dirname(__FILE__). This directory name does not have a trailing slash unless it is the root directory.
so you must use dirname(__FILE__)
__DIR__ gives you the full absolute file system path. You should use this:
dirname($_SERVER['SCRIPT_NAME']);

Set paths for PHP includes on localhost

My new ecom project is in the ecom folder on my mac located here:
"Applications/MAMP/htdocs/ecom"
I access it in the browser by typing localhost/ecom
Problem:
My script fails when I want to include a file, like this:
require('/includes/header.html');
It works when I write it like this:
require('/Applications/MAMP/htdocs/ecom/includes/header.html');
Is there a PHP function I can use to change a setting so that the top version works? Keeping in mind that I have other projects, localhost/wiki, localhost/social that should work in a similar manner.
Many thanks
require('/includes/header.html');
This is an absolute path. If you want to include a PHP file with a path relative to your current file and you don't know or don't care about the full path, use a relative path that does not begin with a /.
Assuming your PHP file is located in /Applications/MAMP/htdocs/ecom and header.html is located in /Applications/MAMP/htdocs/ecom/includes then you need the following code:
require('includes/header.html');
You should ise relative pathes.
/ means the directory beside of /Application
includes/header.html (without the startin /) means the folder relative to my current position. Normally beside the index.php
This is the simple idea use in Joomla / Wordpress / Cakephp & other open source.
Use:
require( dirname( __ FILE __ ).'/includes/header.php');
Let me know if this works.
Thanks,
Munjal

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";

problem referencing file in 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!

Categories