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

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).

Related

Sort a file from the root [duplicate]

This question already has answers here:
PHP absolute path to root
(8 answers)
Closed 2 years ago.
I'm using this code
$sort_data = get_sort('data/profiles/name.cms','¦');
The required "Data" folder is in the root of my host, but the php file that calling name.cms is in sub folder : styles/default/pages/left_member.php
and I'm failing to get that data from the name.cms file, any idea how to call the file from the root?
You could make use of the PHP $_SERVER functionality and target the root directory.
Example:
$sort_data = get_sort($_SERVER['DOCUMENT_ROOT'].'/data/profiles/name.cms','¦');
It's important to note that what's actually defined as the root directory is defined in your configuration file.
'DOCUMENT_ROOT' - "The document root directory under which the current
script is executing, as defined in the server's configuration file."
Source here.
UPDATE AS PER YOUR COMMENT:
Thank you for your help, I already tried it but it's getting whole
xampp directory, so it didn't work.
If you ever take your project "live", the $_SERVER['DOCUMENT_ROOT'] functionality should work. The reason why it doesn't work for you right now, is because you have a "project structure" in your root directory. I.e. localhost/myproject/index.php. What you meant by root is actually the project folder and not the actual root folder.
In that case, you can try 3 different options.
An absolute path without using the PHP reserved variable $_SERVER
to find it.
Example:
$sort_data = get_sort('/data/profiles/name.cms','¦');
Manually manipulate the directory path.
Example:
$sort_data = get_sort('../data/profiles/name.cms','¦');
You can add as many "level up", i.e. ../, as it takes to get to your desired starting point and then locate the folder.
Define your own "root" path variable.
Example:
$my_root = $_SERVER['DOCUMENT_ROOT'].'/myproject';
You can now use $my_root with your path, like so:
$sort_data = get_sort($my_root.'/data/profiles/name.cms','¦');

PHP - unlink('file_name') not working [duplicate]

This question already has answers here:
unlink PHP works when file is in root, not if file is in folder
(2 answers)
Closed 5 years ago.
I have issues while trying to delete files in PHP, using unlink('filename').
I have tried with a complexe file and it didn't worked. I've been using relative paths as adviced on other post about this.
So i've made the simpliest script possible :
<?php
unlink("acs.gif");
?>
The script is located in the same folder as my asc.gif is, tho it still doesn't work.
I've got no fatal errors, and a warning when enabling error_reporting() and init_set(). But the file is still there.
I've tried to set the permissions to both my folder, my image and my script to 0777 but it didn't help.
I'm getting quite confused about what is happening.
Do you guys have any ideas ?
I recommend you to use absulte filepaths. In case you want to delete a file which is in the same directory of the called script, poleteaw answer should work (besides the missing / in the path):
unlink(__DIR__ . '/' . $filename);
Nevertheless take look of php's directory function realpath() and the predefined constants.
So what if you want to delete a file which is not inside your directory:
You can use the realpath() method to generate an absolute path out of a relative path. So realpath('/one/two/three/../..') results in '/one' - or for your case you can do something like realpath(__DIR__ . '/../../') to get into the root directory of you project.
The recommended way is to use a variable which holds the absolute path to the directory where you want to store and administrate your files like $filesDir = '/path/to/my/files'. With this approach you have two wins: your users files do not reside in your php project files and you have a way much better overview of which files are uploaded/administrated.
You shouldn't use relative path to file. If it lies in the same folder as a PHP script, use unlink(__DIR__ . '/' . $filename);. In other cases set a full path to unlink.

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.

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

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

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.

Categories