Relative path problems with PHP

Source: Internet
Author: User
In PHP require,include a file, mostly with a relative path, is a very headache problem.

For example:

<web> (website root directory)

├<a> folder

││

│└1.php

├<b> folder

││

│└2.php

└index.php

Issue: In 1.php through the include (".. /b/2.php ") to introduce 2.php files under the B directory;

Introduce the 1.php file under the A directory by include ("a/1.php") in index.php;

Run out of course there will be problems, not found. /b/2.php file.

Keep in mind a principle that all include statements are converted to include paths that contain file paths as a base, it is recommended to use a basic path as a reference for all paths, such as __file__ or $_server[' Document_root ']

__file__: (Suitable for all situations)

Configure the site root directory in the config file
Define ("Web_root", DirName (__file__));
Config file to the root of the Web site,
Other files contain configuration files,
When you include other files, you can locate them according to Web_root.
Require_once (Web_root. "/a.php");


$_server[' Document_root ', suitable for non-virtual host (Alias) situations

PHP code

<?php  if (!defined ("Wetsite_base_dir"))  define ("Wetsite_base_dir", $_server[' Document_root ']. ' /clare/');      Require_once (wetsite_base_dir. ' includes/global.inc.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.