解决PHP include的路径问题

 

解决PHP include的路径问题

  • Where does include look for file given as a relative path?

(from PHP manual)

Files for including are first looked in include_path relative to the current working directory and then in include_path relative to the directory of current script. E.g. if your include_path is ., current working directory is /www/, you included include/a.php and there is include "b.php" in that file, b.php is first looked in /www/ and then in /www/include/. If filename begins with ./ or ../, it is looked only in include_path relative to the current working directory.

 

  • __FILE__

__FILE__ always equals to the real path of a php script regardless whether it’s included.

__FILE__ helps you specify the file to include using relative path to the including file.

<?php

include dirname(__FILE__).’/subdir’;

//dirname return value does not contain the trailing slash

?>

  • $_SERVER[‘DOCUMENT_ROOT’]

This method allows you to specify a path relative to the web server doc_root for file inclusion.

<?php

if (!defined("WETSITE_BASE_DIR"))

define("WETSITE_BASE_DIR", $_SERVER[‘DOCUMENT_ROOT’].’/Clare/’);

require_once(WETSITE_BASE_DIR.’includes/global.inc.php’);

?>

  • chdir()

The include looks for file relative to current working directory. We can use this feature. It’s really a "fancy" way, but I’m not sure whether it’s safe all the time. Who knows?

rainfalling at yahoo dot com (21-Sep-2005 01:06)

 

This is yet another way to include files relative to the current file. I find it easier if you have a lot of includes.

<?php

$prewd = getcwd(); // get the current working directory

chdir(realpath(dirname(__FILE__))); // change working directory to the location of this file

include(‘includedfile.php’); // include relative to this file

chdir($prewd); // change back to previous working dir

?>

  • set_include_path()

This way is the most convenient way but it’s not without flaws. First, not in all cases you have permission to change server configuration. Second, if there are many path specified in include_path, the actually included file may not be the one you expected because there may be files of the same name under different directories.

  • auto_prepend_file and auto_append_file in php.ini

This almost the best way if your scripts commonly need a startup script. We can do a lot of useful things in the startup script, for examples, define constants, load configurations. But it’s not always OK to change the php.ini settings. Remember the most adaptive application should be as independent from configs as possible.

Advertisements
This entry was posted in PHP Rocks. Bookmark the permalink.

2 Responses to 解决PHP include的路径问题

  1. chinacio says:

    很有用,谢谢!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s