PHP relative path: PHP relative path problem

Source: Internet
Author: User
Tags define config include relative root directory

When you require,include a file in PHP, it's mostly a relative path, and it's a very frustrating problem.
Example:
<web> (site root)
├<a> folder
││
│└1.php
├<b> folder
││
│└2.php
└INDEX.P HP
Problem: In 1.php by include (". /b/2.php ") to introduce 2.php files in the B directory;
to introduce 1.php files in the A directory via include (" a/1.php ") in index.php;
running out of course will cause problems and cannot be found. /b/2.php files.
Remember a principle that all include statements are eventually converted to include paths that are based on the include file path suggest using a basic path as all path references, such as __file__ or $_server[' document_root '
__file__ :(suitable for all situations)
Configure the site root in config file
define ("Web_root", DirName (__file__));
The config file is placed in the root directory of the Web site, and
Other files contain configuration files,
which are positioned according to Web_root when they contain other files.
Require_once (web_root. "/a.php");
$_server[' document_root ', suitable for non-virtual host (Alias) conditions
 

 
  
  
  1. <?php
  2. if (!defined ("Wetsite_base_dir"))
  3. Define ("Wetsite_base_dir", $_server[' Document_root '). ' /clare/');
  4. Require_once (wetsite_base_dir. ' includes/global.inc.php ');
  5. ?>
This article links http://www.cxybl.com/html/wlbc/Php/20130319/37249.html

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.