Ask for advice on a file path

Source: Internet
Author: User
If you encounter a file path problem, ask for advice. I encountered the same problem as the Post author. why | -- images | -- include & amp; nb encountered a file path problem. ask for advice.
I encountered the same problem as the Post author http://hi.baidu.com/linywh/item/1ae8ac335f0d4ef8e6bb7a73
The folder structure of my project is as follows:

|-Images
|-Include
|-Class
|-Config
| -- Function
| -- Index. php
There is a configuration file config in the config folder. the php class folder contains a file mysql. class. php in config. use require_once (".. /class/mysql. class. php ");
Then I used require_once ("include/config. php") in index. php ");
Then it prompts me an error.

Warning: require_once (.. /class/mysql. class. php) [function. require-once]: failed to open stream: No such file or directory in E: \ www \ blog \ include \ config. php on line 13

Fatal error: require_once () [function. require]: Failed opening required '.. /class/mysql. class. php '(include_path = '.; c: \ php5 \ pear ') in E: \ www \ blog \ include \ config. php on line 13
I visited Baidu Google for beginners like me and Cainiao and found a method called dirname (_ FILE _) to obtain the absolute path of the current folder. and dirname () nesting use dirname (_ FILE _) to obtain the previous directory, so I changed my config. phpd require_once statement
Change to require_once (dirname (_ FILE _). "../class/mysql. class. php ");
Then reference config. php in index. php without errors


I followed the POST method and solved the problem, but I still can't understand why there is no error? Here, the two file paths are output as follows:
D: \ web \ graduatesManagement ../class/Database. class. php
D: \ web ../class/Database. class. php
These two paths are hard to understand. please help me to answer them. thank you. Share the file path:
------ Solution --------------------
The include operation can be seen as just inserting the code, no matter how many layers are nested, it will eventually enter the main file
Therefore, the file path is based on the path (execution path) of the main file, and the relative path is also calculated based on this

It is enough to grasp this principle. use _ DIR __,__ FILE __, realpath (), $ _ SERVER, and other constants, variables, and functions.

------ Solution --------------------
To determine where the directory is currently being executed, you can use getcwd to determine where the current execution path is, because sometimes the current path can be changed through chdir.

In addition, as mentioned above, when chdir is not used, it is mainly based on the initial file (usually index. php), that is to say, most of the getcwd paths are equal to the path where the main file is located.

The dirname (_ FILE _) is the path of the current execution FILE. Based on this, the absolute path is combined, which has no relationship with the path of the main FILE, but an absolute path.

Therefore, we recommend that you do not use relative paths if you can use absolute paths in future code.
Related Article

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.