Home > Article > Backend Development > PHP include file path problem
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('ROOT_PATH',dirname(__FILE__)); define('ACTION_PATH',ROOT_PATH.'/action/'); define('BEAN_PATH',ROOT_PATH.'/bean/'); define('DAO_PATH',ROOT_PATH.'/dao/'); define('IPADVIEW_PATH',ROOT_PATH.'/ipadView/'); define('VIEW_PATH',ROOT_PATH.'/view/'); ?>