Home  >  Article  >  Backend Development  >  PHP include file path problem

PHP include file path problem

巴扎黑
巴扎黑Original
2016-12-01 11:06:411512browse

I have just been learning PHP recently, and I am working on a small project. I use a lot of require and include, and I am confused by absolute paths and relative paths. I finally figured it out.

1. The relative path of php is based on the running script. For example, A contains B and B contains C. If A and C are in the same directory, the path containing C in B should be relative to A. If B , C are in the same level directory, then the path containing C in B is relative to C.

2. No matter how the .php script is included, dirname(__FILE__) gets the absolute path of the folder where the script is located.

It is difficult to apply these two rules accurately, at least I often get confused myself, so I used a trickier method and wrote a path.php myself, so that I rarely make mistakes.

 <?php
      define(&#39;ROOT_PATH&#39;,dirname(__FILE__));
      define(&#39;ACTION_PATH&#39;,ROOT_PATH.&#39;/action/&#39;);
      define(&#39;BEAN_PATH&#39;,ROOT_PATH.&#39;/bean/&#39;);
      define(&#39;DAO_PATH&#39;,ROOT_PATH.&#39;/dao/&#39;);
      define(&#39;IPADVIEW_PATH&#39;,ROOT_PATH.&#39;/ipadView/&#39;);
      define(&#39;VIEW_PATH&#39;,ROOT_PATH.&#39;/view/&#39;);
    ?>


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn