My development and production sites written in php both need to use directory iterator in order to get at some files. Directory iterator starts at the base directory of the drive i.e c:/. However, on the dev and prod servers the webroot folder is located in a different place.
Is there a way I can get directory iterator to start at the webroot. Or some similar method I can use so that I can use the same code on dev and prod without having to worry about where on the disk the application is stored.
A DirectoryIterator is instantiated with a $path. Just change that to the webroot.
DirectoryIterator::__construct() ( string $path )
path: The path of the directory to traverse.
You can store the path to the webroot in a config file per environment or determine it at runtime and save it as a constant or in a registry or other accessible place during bootstrap. For instance, if you call your direct access to you application through a FrontController that resides in index.php in the webroot, you can do:
$root = dirname(__FILENAME__);
and store that in a constant or a Registry or something.
Starting the path with getenv("DOCUMENT_ROOT") works
Related
I'm using apahce server and I have files that I want to keep private. I've read that they should be put in a directory outside of the document root.
I could access them from the root directory using:
<?php include('../includes/somefile.php');?>
To simplify navigation I want to use paths relative to the root. This works fine for directories within the site such as:
define('ROOT_PATH', $_SERVER['DOCUMENT_ROOT']);
include(ROOT_PATH.'/path/file.php');
but I get errors when I try going up the directory tree like this:
include(ROOT_PATH.'../includes/somefile.php');
Am I doing something wrong here?
This is because .. is taking to the previous directory of the DOCUMENT_ROOT which you do not have permission to access.
I have a little problem: I began a project as a subdirectory in a larger web project. Thus the web file path is something like /../myProject. But things have progressed and I've realized that this should be its own project. However, I'd like to be able to keep it where it (as a sub-directory) also make it a sub-domain wherein myProject becomes the root. (There is also the possibility that my project will be mirrored at a library site, where it will once be in a sub-directory).
The problem I having with all this is that in some cases I have html_partial files, (for instance for the header or footer). But the relative path of these partials differs depending on where you are in the file tree. I originally solved this by always going back to the root.
But now, you see, depending on where my project lives, the root will be different. What I'd like to do is declare myProject as the "application root" and then be able to use relative paths based on this application root rather the than the web root'. This way, all of the relative paths within 'myProject' will work no matter wheremyProject` lives in the web path.
Does PHP have a way to declare something like an Application Root if so, can you explain it me or direct me to its documentation. Thanks!
You could simply have a PHP file in your application root directory which would define the directory it is in as the application root. The file could be as simple as this:
<?php
define('APPLICATION_ROOT', __DIR__);
?>
You could then include this file as needed and base all of your file paths off of APPLICATION_ROOT. Note that APPLICATION_ROOT would not have a trailing slash as defined here (unless your file happened to be on in the machines root directory, which is unlikely).
I usually do something lile this in the front controller:
define('APPLICATION_PATH', realpath(__DIR__));
Then you can do things like:
set_include_path(APPLICATION_PATH . '/include');
Or:
$fp = fopen(APPLICATION_PATH . '/path/to/some/file', 'r');
If your app doesn't make use of a front controller, you could define an environment variable in your vhost config or .htaccess:
SetEnv APPLICATION_PATH /full/path/to/my/app
And then use:
getenv('APPLICATION_PATH')
I'm having problems with my include files. I don't seem to be able to figure out how to construct my URLs when I use require_once('somefile.php'). If I try to use an include file in more than one place where the directory structures are different, I get an error that the include file cannot be found.
In asp.net, to get my application root path, I can use ~/directory/file.aspx. The tild forward slash always knows that I am referencing from my website root and find the file no matter where the request comes from within my website. It always refers back to the root and looks for the file from there.
QUESTION: How can I get the root path of my site? How can I do this so I can reuse my include files from anywhere within my site? Do I have to use absolute paths in my URLs?
Thank you!
There is $_SERVER['DOCUMENT_ROOT'] that should have the root path to your web server.
Edit: If you look at most major php programs. When using the installer, you usually enter in the full path to the the application folder. The installer will just put that in a config file that is included in the entire application. One option is to use an auto prepend file to set the variable. another option is to just include_once() the config file on every page you need it. Last option I would suggest is to write you application using bootstrapping which is where you funnel all requests through one file (usually with url_rewrite). This allows you to easily set/include config variables in one spot and have them be available throughout all the scripts.
I usually store config.php file in ROOT directory, and in config.php I write:
define('ROOT_DIR', __DIR__);
And then just use ROOT_DIR constant in all other scripts.
Using $_SERVER['DOCUMENT_ROOT'] is not very good because:
It's not always matching ROOT_DIR
This variable is not available in CGI mode (e.x. if you run your scripts by CRON)
It's nice to be able to use the same code at the top of every script and know that your page will load properly, even if you are in a subdirectory. I use this, which relies on you knowing what your root directory is called (typically, 'htdocs' or 'public_html':
defined('SITEROOT') or define('SITEROOT', substr($_SERVER['DOCUMENT_ROOT'], 0, strrpos($_SERVER['DOCUMENT_ROOT'], 'public_html')) . 'public_html');
With SITEROOT defined consistently, you can then access a config file and/or page components without adapting paths on a script-by-script basis e.g. to a config file stored outside your root folder:
require_once SITEROOT . "/../config.php";
You should use the built in magic constants to find files. __FILE__ and __DIR__. If you are on PHP < 5.3 you should use dirname(__FILE__)
E.g.
require_once __DIR__.'/../../include_me.php';
$_SERVER['DOCUMENT_ROOT'] is not always guaranteed to return what you would expect.
Define it in a config file somewhere.
Assuming you're using an MVC style where everything gets routed through a single index.php then
realpath('.');
Will show you the path to the current working directory (i.e where index.php is)
So then you can define this as
define('PROJECT_ROOT', realpath('.'));
If it's not MVC and you need it to work for files in subfolders then you can just hard code it in a config file
define('PROJECT_ROOT', 'C:/wamp/www/mysite');
Then when including something you can do;
include PROJECT_ROOT . '/path/to/include.php';
You could alternativly set the base directory in your .htaccess file
SetEnv BASE_PATH C:/wamp/www/mysite/
Then in PHP you can reference it with $_SERVER['BASE_PATH']
Try this:
$_SERVER['DOCUMENT_ROOT']
The directory and file structure is as follows:
C:\xampp\htdocs\PHP_Upload_Image_MKDIR\uploaded
C:\xampp\htdocs\testA.php // as follows
$userID = 's002';
$uploadFolder = '/PHP_Upload_Image_MKDIR/uploaded/';
$userDir = $uploadFolder . $userID;
mkdir($userDir, 0700);
If I call testA.php, then the following folder will be created.
C:\PHP_Upload_Image_MKDIR\uploaded\s002
However, the desired result should be the following:
C:\xampp\htdocs\PHP_Upload_Image_MKDIR\uploaded\s002
I would like to know a decent method so that the mkdir creates folder relative to the root of the web
C:\xampp\htdocs\
or
C:\wamp\www
Then in the future, I don't have problems to move this application to a web hosting site.
Thank you
You can:
Create a file with defines that's always included and where you define what's the server root (or whatever prefix) so that you prepend it to the directory.
Use a relative directory to the script. The directory of the script can be obtained with dirname(__FILE__).
Use a relative path to the current directory. The current directory, if not changed, is usually the directory of the PHP script that was originally called (usually not a good option).
Use $_SERVER[DOCUMENT_ROOT] (not a very good option, you might want your application in a subdirectory, and whether this value is available depends on the web server).
$uploadFolder = $_SERVER['DOCUMENT_ROOT'] . '/PHP_Upload_Image_MKDIR/uploaded/';
My situation: I have a class that I want to use in a number of places, so I placed it high in the directory tree:
/www/libs/classA/classA.php
It uses a config file located in the same directory called config.ini and references it locally in the constructor. However, when I include it from a script in a different directory, it tries to load the config file from the directory the calling script is in. Is there any way I can look in the directory the included file is in? Ideally I could move the script to a different directory and not make any changes to it.
You should use the absolute path when including/loading files. And you can retrieve that absolute path with the __FILE__ constant and dirname function:
dirname(__FILE__).'/config.ini'
This will five you the absolute path of the config.ini file in the same directory the currently executed script file is located in. So when that code is executed in your classA.php script file, you would get /www/libs/classA/config.ini.
You should be able to include/require a file from the current file's directory if you have no path info in the argument, e.g. include('file2.php');. But I think even if you have './file2.php'), then it's going to become relative to the directory of the first-included/executed script.
I like the dirname(____FILE____)... method, but I believe in the case where a certain set of files is intended to always be deployed in the same directory, the direct file include promotes cohesion.