Sort a file from the root [duplicate] - php

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','¦');

Related

What is the difference between / addressing and ./ addressing? [duplicate]

This question already has answers here:
What is double dot(..) and single dot(.) in Linux?
(4 answers)
Closed 4 years ago.
I am working on a PHP website where I needed to get a list of the files in the Images directory. The example in PHP.net shows the addressing with just a forward slash as shown in the example. This addressing pattern did not work. Instead the address needs to start with ./ . What is the difference?
<?php
$dir = '/tmp';
$files1 = scandir($dir);
$files2 = scandir($dir, 1);
print_r($files1);
print_r($files2);
?>
A search for other questions have addressed single dot and double dot, but I don't see posts for a forward slash with no dot.
the . indicates the current directory. Assume, for instance, that your current path is /var/www:
If you say ./tmp the directory you are looking for is /var/www/tmp
If you say /tmp you are looking for /tmp (starting from the root)
Maybe you're a Windows user and you are not familiar with the way Linux addresses files, but essentially there are no drive letters. / is the root of the file system, basically equivalent to C:\ on Windows (though not quite). It also works on Windows except it will refer to the root of the partition your script is running in as Windows separates each partition on a different drive letter.
There's a standard folder in the root of the file system called tmp, and if you want to refer to it you want to specify full path, /tmp.
Just using tmp or ./tmp will refer to a tmp folder on your local path, which is a completely different folder just with the same name (that might not even exist).
When you don't want to specify a full path but somewhere relative or local path instead, you don't put / in the beginning. You might just put nothing, or you can put ./ to explicitate this is a path relative to your current working directory which is ..
It doesn't matter in which folder your script working, . always represents it. It is actually unnecessary to put ./ in most cases since relative paths are implicit.
On a relate note, .. represents the parent folder.
Example, my script is /var/www/script.php, when I run it:
. is the folder /var/www.
fopen('note.txt', 'r') will open file /var/www/note.txt for reading.
fopen('./note.txt', 'r') will do the exact same thing.
.. is the folder /var.
../../tmp is the same as /tmp.
Note the current working directory represented by . remains constant even if you include() a script from a subfolder.
This might get things confusing because . may not be the folder the script you included is in. To workaround this problem, use __DIR__ to get the folder your script is in, it will get the current folder of your script even if you are calling it through a include() or calling it from a different directory.
/ denotes the root directory of your server. As in /var/www/.
./ denotes the current directory that the script is executing in. This is the equivalent of getcwd(). For example, you're currently in /php/scripts, it will refer to /var/www/php/scripts.
../ denotes the parent folder, and can be chained to move up multiple levels.
The use of each depends on which file or folder you're trying to manipulate from which script.
If you're trying to access /var/www/tmp from /var/www/php/scripts/script.php, you can use either:
../../tmp
/tmp
Note that if you're unsure how many levels you need to 'move up', you can affix an infinite number of ../; it won't 'break out' of the root. Having said that, referencing directly from the root is often much more straight-forward.

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

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

Categories