Include path modification in PHP

Source: Internet
Author: User

1.__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.
This method is the preferred recommendation. Although your include statement will therefore write longer, but a word, value!

<? PHP include dirname (__file__). ' /subdir '; // dirname return value does not contain the trailing slash?>

2.$_server[' Document_root ']

This method allows the Specify a path relative to the Web server Doc_root for file inclusion.
This is also a good way for many projects to be adopted.

<? PHP if (! defined ("Wetsite_base_dir")) Define $_server [' Document_root ']. ' /clare/'); require_once (Wetsite_base_dir. ' includes/global.inc.php ');? >

3.chdir ()

the include looks for file relative to current working directory. We can use the This feature. It's really a "fancy", but I'm not sure whether it's safe all the time. Who knows? 
this way feels a little bit troublesome, it's not easy to remember to restore the working directory at any time. After writing this sentence, I then wrote several test files and found that the most important disadvantage of this approach was not trouble, but in its side effects: Changing the working directory, which could lead to procedural logic errors.

 <? php   $prewd  = GETCWD  (); //  Get the current working directory  chdir  (realpath  (dirname  (__file__ )); //  working directory to the location of this file  include  (' includedfile.php '); //  include relative to the This file  chdir  ( $prewd ); // ? 

4.set_include_path ()

This is the most convenient way, but not without the disadvantage. First of all, sometimes you don't necessarily have permission to modify the configuration. Second, you will be confused (even if you don't, your maintainer) when the filenames are duplicated under different paths.

5.auto_prepend_file and Auto_append_file in php.ini

This is almost the best approach if you need to include a generic script for each script, but the downside is that it's not configuration-dependent enough to be independent.

Include path modification in PHP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.