note 94392 added to function.include

From: Date: Mon, 02 Nov 2009 10:12:28 +0000
Subject: note 94392 added to function.include
Groups: php.notes 
Request: Send a blank email to php-notes+get-162328@lists.php.net to get a copy of this message
I would like to point out the difference in behavior in IIS/Windows and Apache/Unix (not sure about any others, but I would think that any server under Windows will be have the same as IIS/Windows and any server under Unix will behave the same as Apache/Unix) when it comes to path specified for included files. Consider the following: <?php include '/Path/To/File.php'; ?> In IIS/Windows, the file is looked for at the root of the virtual host (we'll say C:\Server\Sites\MySite) since the path began with a forward slash. This behavior works in HTML under all platforms because browsers interpret the / as the root of the server. However, Unix file/folder structuring is a little different. The / represents the root of the hard drive or current hard drive partition. In other words, it would basically be looking for root:/Path/To/File.php instead of serverRoot:/Path/To/File.php (which we'll say is /usr/var/www/htdocs). Thusly, an error/warning would be thrown because the path doesn't exist in the root path. I just thought I'd mention that. It will definitely save some trouble for those users who work under Windows and transport their applications to an Unix-based server. A work around would be something like: <?php $documentRoot = null; if (isset($_SERVER['DOCUMENT_ROOT'])) { $documentRoot = $_SERVER['DOCUMENT_ROOT']; if (strstr($documentRoot, '/') || strstr($documentRoot, '\\')) { if (strstr($documentRoot, '/')) { $documentRoot = str_replace('/', DIRECTORY_SEPARATOR, $documentRoot); } elseif (strstr($documentRoot, '\\')) { $documentRoot = str_replace('\\', DIRECTORY_SEPARATOR, $documentRoot); } } if (preg_match('/[^\\/]{1}\\[^\\/]{1}/', $documentRoot)) { $documentRoot = preg_replace('/([^\\/]{1})\\([^\\/]{1})/', '\\1DIR_SEP\\2', $documentRoot); $documentRoot = str_replace('DIR_SEP', '\\\\', $documentRoot); } } else { /** * I usually store this file in the Includes folder at the root of my * virtual host. This can be changed to wherever you store this file. * * Example: * If you store this file in the Application/Settings/DocRoot folder at the * base of your site, you would change this array to include each of those * folders. * * <code> * $directories = array( * 'Application', * 'Settings', * 'DocRoot' * ); * </code> */ $directories = array( 'Includes' ); if (defined('__DIR__')) { $currentDirectory = __DIR__; } else { $currentDirectory = dirname(__FILE__); } $currentDirectory = rtrim($currentDirectory, DIRECTORY_SEPARATOR); $currentDirectory = $currentDirectory . DIRECTORY_SEPARATOR; foreach ($directories as $directory) { $currentDirectory = str_replace( DIRECTORY_SEPARATOR . $directory . DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR, $currentDirectory ); } $currentDirectory = rtrim($currentDirectory, DIRECTORY_SEPARATOR); } define('SERVER_DOC_ROOT', $documentRoot); ?> Using this file, you can include files using the defined SERVER_DOC_ROOT constant and each file included that way will be included from the correct location and no errors/warnings will be thrown. Example: <?php include SERVER_DOC_ROOT . '/Path/To/File.php'; ?> ---- Server IP: 69.147.83.197 Probable Submitter: 207.98.219.80 ---- Manual Page -- http://www.php.net/manual/en/function.include.php Edit -- https://master.php.net/note/edit/94392 Del: integrated -- https://master.php.net/note/delete/94392/integrated Del: useless -- https://master.php.net/note/delete/94392/useless Del: bad code -- https://master.php.net/note/delete/94392/bad+code Del: spam -- https://master.php.net/note/delete/94392/spam Del: non-english -- https://master.php.net/note/delete/94392/non-english Del: in docs -- https://master.php.net/note/delete/94392/in+docs Del: other reasons-- https://master.php.net/note/delete/94392 Reject -- https://master.php.net/note/reject/94392 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#162328) next »