The most effective way to solve the PHP relative directory problem

Source: Internet
Author: User
Tags relative

Data collection and multiple tests have found that relative paths in other Web programming languages are based on the current processing file directory, not in PHP. The relative path in PHP is based on the current working directory, not on the current processing file directory, which is causing a lot of trouble during the development process.

When you include a file with include or Requice, the file path is preceded by a dirname (__file__). For example: Existing a.php, b.php, c.php, D.PHP4 files, they relate to:

B.php (same level as root directory)
Root directory/a.php
Root directory/c.php
Root directory/dir/d.php

Now include b.php, c.php in the a.php. The wording is as follows:

<?php
Include_once (DirName (__file__). " /.. /b.php ");
Include_once (DirName (__file__). " /c.php ");
?>

If you want to include d.php in c.php. The wording is as follows:


<?php
Include_once (DirName (__file__). " /dir/d.php ");
?>

This approach is currently the most effective way to solve the problem of path dislocation.

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.