PHP refer to certain location in Filesystem (../ or ./) [duplicate] - php

This question already has answers here:
What does the dot-slash do to PHP include calls?
(7 answers)
Closed 7 years ago.
What's the effective difference between:
include_once("../backend/example.php");
and
include_once("./backend/example.php");
My problem is that on my development-environment (XAMPP-Server) i had to use "./". But when i tried to upload my progress to the production-server, i had to change the paths to "../".
Thanks for your answer!

./ refers to the current working directory (It's basically redundant, but it reiterates that you're starting in the current directory, and not at the root / folder, or in PHP's case it may try other folders to find the file.). To see which directory that is, you can use the getcwd() function. ../ basically instructs php to go back to the parent folder and then into backend/example.php.
A few examples:
Let's say this is your root, and your cwd: /var/www/mywebsite/
../backend/example.php would refer to: /var/www/backend/example.php
./backend/example.php (And also just backend/example.php) would refer to: /var/www/mywebsite/backend/example.php

./= Actual folder
../= one folder back
you can define website path in config
like this
$path = 'http://yourwebsite.com'
Than you can do like this :)
include($path"/backend/example.php");

Related

What it mean when we perfix with dots for require function to indicate filename in php [duplicate]

This question already has answers here:
What is double dot(..) and single dot(.) in Linux?
(4 answers)
Closed 5 years ago.
I always use require('dbc.php'); to include file but what is the difference when I prefix 2 dots ../ as below, is there is any extra security.
require('../dbc.php');
require('../lib/bootstrap.php');
require_once '../../../conf/config.php';
If you do
../../
You've gone back two directory
../
You've gone back one directory
This basically going out the current directory the file u are working on is in. It depends on the location of the db file relative to the file that needs it. It has nothing to do with security.
The . gives you the ability to set the path of the included files relatively to the path of the original file that run (the file that included them). The ./ indicates the current directory. So if including a file like such:
require('./config.php')
You are telling PHP to look in the current directory for "config.php". Which is the same as
require('config.php')
The ../ indicates the directory above or "parent directory"
require('../dbc.php');
This is telling PHP to go one directory up and look for "dbc.php".
These commands can be chained like so:
require('../lib/bootstrap.php');
require_once '../../../conf/config.php';
The dots simply are used to traverse the directory structure. What is double dot(..) and single dot(.) in Linux?, though you should avoid using relative paths and use absolute paths. Absolute vs. relative paths.
Security:
In its self, it introduces no security benefits, except if you get it wrong your app won't work at all!
It does add some protection against code disclosure if PHP fails to parse. This applies ONLY if you store your main code outside of the webroot, though I have never encountered or seen this issue spontaneously happen, though it possibly could. Storing script files outside web root.

relative vs absolute path of a file for php includes [duplicate]

This question already has answers here:
Get relative path to a parent directory
(2 answers)
Closed 8 years ago.
So I have this sort of file set up. /cloud/ and /embed/ being two different subdomains.
www
-cloud
--config.php
--files
---formSubmit.php
-embed
--index.php
in /embed/index.php I have the following code:
include("/www/cloud/files/formSubmit.php");
in /cloud/files/formSubmit.php I have the following code:
include("../config.php");
If I am on cloud.website.com and I go to the formSubmit.php, everything works fine and the config file is included.
However, If I am on embed.website.com and I go to the index.php, I get an error saying that config.php was not found.
Does anyone know what do I need to do to include my formSubmit.php from either location and have my config.php included?
In this case, it seems your usage of relative paths is working and absolute paths are not. Whether that means the absolute path of /www/cloud/files/ is incorrect or not, I do not know. In my code, I tend to try to reference files relatively as much as possible like so:
// In embed/index.php
include_once dirname(dirname(__FILE__)) . '/cloud/files/formSubmit.php';
What that does is get the directory of the currently executing file and then it's parent directory, which would be www, and then goes back down the path from there to the file I need.
Subdomains should not make a difference when accessing files server side (as long as the files are hosted on the same server).

PHP special symlink usage [duplicate]

This question already has answers here:
How to get the relative directory no matter from where it's included in PHP?
(5 answers)
Closed 8 years ago.
I have the following test structure:
/www/index.php
<?php
require_once(dirname(__FILE__).'/linked/linked.php');
/www/linked/ which is a symlink to /symlinkedfolder/
/symlinkedfolder/linked.php
<?php
echo __FILE__;
The output for this script is:
/symlinkedfolder/linked.php
Is there any way/technique with PHP or Apache or Linux which would make symlink behave not symlink instead like a normal filesystem folder/file?
I need that my example give back the following output:
/www/linked/linked.php
(But in real it would be still a symlinked file which originally located in its original folder)
UPDATE #1
We are working with version control system and we would like to keep the checked out folder in a global folder and we would like to symlink each folders to its proper path in the actual platform(Joomla or WordPress etc...). It would allow us to only update and commit from one folder, but still refresh every platform with a single update. (This could work until we not use FILE or DIR or any related things what symlink can mix up.)
It’s a pain. As the official PHP documentation explains:
The full path and filename of the file. If used inside an include, the
name of the included file is returned. Since PHP 4.0.2, FILE
always contains an absolute path with symlinks resolved whereas in
older versions it contained relative path under some circumstances.
Which is a pain. This is why I have decided it’s best to set a base path explicitly as I explain here. So in your case you would set:
$BASE_PATH = '/www/';
And then your require_once would be like this:
require($BASE_PATH . '/linked/linked.php');
This question & answer is similar to yours and recommends using $_SERVER['SCRIPT_NAME'] but in my experience, that setting can change radically between server to server for odd reasons. Which is why I have defaulted to the $BASE_PATH method when I code. You set it once, forget it & no worries.

Difference between ./ and ../ in PHP [duplicate]

This question already has answers here:
What does the dot-slash do to PHP include calls?
(7 answers)
Closed 9 years ago.
I wanted to ask whats the difference between
include('./config.php')
and
include('../config.php')
The 2nd one goes one dir up and includes from folder one level higher. But what Exacly does one dot do?
Edit; Thank you everyone for you answers. It brightened my mind a bit. I chose answer that was most infomative about topic (especially in PHP enviroment)
In posix file systems . simply means current directory and .. means parent directory.
This is a system thing and NOT just a PHP thing.
The ./ indicates the current directory. If you ever list the contents of a *nix system you will get the following at the top.
.
../
The top one (.) is the same as ./ which means "this directory". So if including a file like such:
include('./config.php')
You are telling PHP to look in the current directory for "config.php". Which is the same as
include('config.php')
The ../ indicates the directory above or "parent directory"
include('../config.php')
This is telling PHP to go one directory up and look for "config.php". These commands can be chained like so:
../../config.php
This tells the system to go up one directory, go up again and then look for "config.php"
One dot refers to the current directory.
include('./config.php')
is basically the same as
include('config.php')
. means the current directory
.. means the parent directory
It is not about PHP, it is OS convention that is called dot directory name
"dirA\.\dirB" is equivalent to "dirA\dirB".
"dirA\dirB\..\dirC" is equivalent to "dirA\dirC".
include('../config.php') :- parent directory
include('./config.php') :- current directory
./ is for the current directory
../ is for going to the parent directory of the current one
For answering your question, suppose you don't write .
include('/config.php') would include the 'config.php' file in the root of the filesystem

How do I change where PHP looks for user defined includes? [duplicate]

This question already has answers here:
Nested php includes using './'
(6 answers)
Closed 8 years ago.
I'm about to go and change all my includes to use get current working directory to specify where to look.
getcwd()
because PHP does not look in the current directory as I thought it would.
Is there a way to change this behavior so my code is not littered with getcwd() everywhere?
A way to tell the inlcude function to look in the directory in which it itself is located.
I thought out of common sense it would do this but it does not.
Set your include_path in PHP.ini.
Or, use set_include_path().
http://php.net/manual/en/function.set-include-path.php

Categories