PHP site-relative inclusion, path problem Resolution (Include,require)

Source: Internet
Author: User

Previously seen, many frameworks, mostly rarely used relative path inclusions. and generally many do PHP Web site, like to use relative path. Think so, no matter where the directory is put. As long as the other directory relationship is consistent. Then there will be no problem. If a site, generally think, if you use the root directory, will often change the site address, feel very inconvenient. In fact, we will find from the most common framework, basically is the absolute path method is adopted.

    • Relative paths cause problems

We have directories of the following structure.

<web> (Web root directory) ├<a> folder │││└a.php├<b> folder │││└b.php└test.php

If b.php contains a.php (Include (". /a/a.php "), and then test.php contains b.php (Include (" b/b.php "), we find it strange.

First access: b.php can be accessed normally, then access test.php

found that the a.php had not been found. One thing to note here is that thedefault relative path for PHP is the path where the page is accessed. Regardless of the entry page, which contains the number of files, relative to the path, the page will prevail .

If the access test.php root path is: test.php, if the access b.php relative path is based on the path where b.php is located. Just page test.php contains the b.php,b.php containing the a.php. All inclusions are subject to test.php.

It is estimated that PHP has just started learning friends, often encountered this problem, and found that often a lot of warnings affect the interest of learning.

    • Using the absolute path method

The main open source framework uses an absolute path approach, which avoids relative paths because the inclusion of access to the file has changed. The reference path changes so that the inclusion error has occurred. So, let's look at common methods.

First, set the site benchmark to a fixed file. Generally, the following methods can be implemented. For example, there is a config.php file under the root directory.

<?php

Define (' Root_path ',dirname (__file__));

__file__ to the current script path, in that footstep PHP called the variable, its value is the absolute path of the footstep.

Then, any other page, at the time of the inclusion, only needs to include the config.php.

<?php

Contains config.php .....

Include (Root_path. " /file path ");

    • Using absolute path Benefits

The use of the solve path benefits in addition to large projects, including the time to more accurately locate the file, not easy to produce errors. There is another benefit, including files, that performance can be greatly improved.

If a relative location is included, PHP looks for the file, usually in the Set_include_path function, the set of all paths to search. We know that you want to try it one after another, enumerate the directories, and then look for the files. This consumes a large IO directly. Also consumes a lot of performance. If we use absolute inclusion, we can determine exactly if the file exists. Will not go to Set_include_path set directory to find out.

The above issues, for just contact with PHP large project development, may be very easy to encounter. Welcome to the discussion!

No related articles!

PHP site-relative inclusion, path problem Resolution (Include,require)

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.