Reputation: 101
Currently I am creating a dir by using ../
to go one step back
mkdir("../"."src/new");
this is work fine but is this any other way to give a full path rather then using ../
everywhere
Upvotes: 0
Views: 1289
Reputation: 447
Giacomo has a good answer and that would work well. However, I think a better way is to define a path at your entry point for the root directory.
define('ROOT', dirname(__DIR__) . DIRECTORY_SEPARATOR);
This will give you a variable 'ROOT' that you can use to access your project path from anywhere in your application without having to explicitly write out the path. You can test it simply by using
echo ROOT;
Hope this helps anyone down the road.
Upvotes: 0
Reputation: 26066
This is work fine but is this any other way to give a full path rather then using
../
everywhere.
Yes. Just set a PHP configuration variable called something like $BASE_PATH
in your PHP and then prepend that to the mkdir
like this:
$BASE_PATH = '/full/path/to/your/codebase/here/';
If you want to determine what your site’s base path is, just place this line of code in the main index of your site; such as in index.php
:
echo "Your path is: " . realpath(dirname(__FILE__)) . "<br />";
Then load that page. Somewhere near the top will be this text:
Your path is: /full/path/to/your/codebase/here/
And that will be the base path to your code on whatever system you are checking this out on.
Then can take that $BASE_PATH
setting and place it in a PHP file named something like local.php
. And then you just make sure all of your PHP files have a line like this near the top of the file like this:
require_once('local.php');
Or this:
require_once('../local.php');
Or ever this:
require_once('../../local.php');
Yes, it seems like you are trading the ../
in your mkdir
for this in a config file, but the benefit is you only have to set those require_once
calls once in your PHP files. Once that is done, you can just change $BASE_PATH
in one place & not have to worry about it.
And now when you make calls to the file system in your code, do this:
mkdir($BASE_PATH . 'src/new');
What is nice about a setup like this is that you can make your app portable between environments by just changing $BASE_PATH
to match your local environment.
if ($app_environment == 'local') {
$BASE_PATH = '/full/path/to/your/codebase/here/';
}
else {
$BASE_PATH = '/var/www/your/remote/site/codebase/here/';
}
Some people will claim that $_SERVER["DOCUMENT_ROOT"]
and such would work, but the reality is this automatted method don’t work well form server to server. Very inconsistent behavior. It’s always best to set a $BASE_PATH
and then use that rather than fumble around with anything else.
Upvotes: 1
Reputation: 55
You could set up a variable in a page that is included in all pages like $fullpath=
$_SERVER["DOCUMENT_ROOT"]
Upvotes: 0
Reputation: 59661
Try realpath
. From the docs:
realpath() expands all symbolic links and resolves references to '/./', '/../' and extra '/' characters in the input path and returns the canonicalized absolute pathname.
If you want the parent directory of your current directory, then use this:
//print out parent directory
$parent = dirname(getcwd());
echo $parent;
Upvotes: 2
Reputation: 574
Try the below one. getcwd() will give the physical path.
$path = getcwd().'your path';
Upvotes: 2