The problem of PHP path and its solving method

Source: Internet
Author: User
Tags pear

When we were beginners in PHP, we were often bewildered by the absolute path and relative path of PHP. Path problems often cause the include and require commands not to load onto the specified page, causing the Web program to run an error. This article will be with you to explore the PHP path problem of common solutions. ­

Let's start with a simple example:

­

The diagram above is a structure diagram of a simple Web project, where the code for each file is as follows:

root/index.php:

<?php

Require_once ' app/blog.php ';

­

$blog =new Blog ();

echo $blog->getbloginfo ();

?>

root/app/blog.php: [/b]

<?php

Require_once '.. /lib/smarty/functions.php ';

?>

<?php

Class Blog

{ ­

Public Function Getbloginfo ()

Return ' is a test ' of blog! '; ­

} ­

?>

<?php

Echo ' root/app/blog.php loaded successed!</br> '; ­

?>

First, we enter http://localhost/root/app/blog.php directly from the browser, and the browser displays:

———————————————————————-

root/app/blog.php Loaded successed!

———————————————————————-

The file was loaded successfully and no warnings or errors occurred. ­

Next, we request the following Web site in the browser: http://localhost/root/index.php, the browser displays the following error message:

———————————————————————————

Warning:require_once (.. /lib/smarty/functions.php) [Function.require-once]: failed to open stream:no such file or directory in t:/study/php_rel/p Rojects/root/app/blog.php on line 2

Fatal error:require_once () [function.require]: Failed opening required '. /lib/smarty/functions.php ' (include_path= '.; C:/php5/pear ') in t:/study/php_rel/projects/root/app/blog.php on line 2

———————————————————————————

Why '.. /lib/smarty/functions.php ' failed to load? There is no exception to our access to http://localhost/root/app/blog.php, which suggests that the cause of the error is likely to be in root/index.php 's require_once ' app/blog.php Statement Search from Google: php path problem [/b], you can find the answer to the question. When root/index.php references root/app/blog.php , the reference point of the require_once statement in blog.php becomes root/index.php[/b] The directory in which it resides (root directory root). So, take root/index.php as the reference point to load '. /lib/smarty/functions.php ', naturally it went wrong. ­

Find the problem, and then we'll find a way to solve the problem:

1. Put all the files under a folder

If you put all the files under one folder, there is no path problem. But this is definitely a bad idea. Unless you write a small project that can no longer be small, please do not try this method, the system without structure is terrible. ­

2. Use absolute path

Note: The Include and require in PHP use the absolute path of the file system, such as "c:/wwwroot/yourproject/index.php"

­

Step1: The exception just happened because we used a relative path, and if you change to an absolute path, the above error will not occur. Let's simply revise the root/app/blog.php:

The first three lines are set by the

<?php

Require_once '.. /lib/smarty/functions.php ';

?>

­

Modified to:

<?php

Require_once ' t://study//php_rel//projects//root//lib//smarty//functions.php ';

?>

Now, we visit: http://localhost/root/index.php, browser display:

—————————————-

root/app/blog.php Loaded successed!

This is a test of blog!

—————————————-

Program execution was successful. ­

Step2: Although the program does not complain, it is clear that we do not really solve the problem. No one will write in the program require_once ' t:/study/php_rel/projects...functions.php ' such things, such writing will make the program completely lose flexibility, make the program difficult to transplant!

Let's recall the reason for the error: 1. References between different levels of files have changed the reference point of the require_once; 2. When the reference point changes, loading the file by relative path is an error. If we fixed the reference point of the require_once, the problem would not be solved. How do I fix a reference point? The absolute path is used to implement the method: a function + a constant: dirname () and __file__. Let's revise the root/app/blog.php as follows:

The first three lines are set by the

<?php

Require_once ' t://study//php_rel//projects//root//lib//smarty//functions.php '; ­

?>

Modified to:

<?php

Require_once dirname (__file__). ' /’.‘.. /lib/smarty/functions.php '; ­

?>

Access to the http://localhost/root/index.php program works fine, but the solution is obviously better than the solution in Step1. It should be said that this is a "absolute path + relative path" approach. ­

­

Step3: The above method has been able to solve the path problem, but the feeling code is not elegant. To make the code more elegant, we can do this: create a new settings.php under root (root):

­

root/settings.php: [/b]

<?php

if (!defined (Abspath))

Define (' Abspath ', DirName (__file__). /’);

?>

The code in root/app/blog.php is modified to:

<?php

Require_once Abspath. ' Lib/smarty/functions.php '; ­

?>

The code in root/index.php is also modified to:

­

<?php

Require_once ' settings.php '; ­

Require_once Abspath. ' App/blog.php ';

­

$blog =new Blog ();

echo $blog->getbloginfo ();

?>

If you think about it, you will have a problem with direct access to http://localhost/root/app/blog.php: The constant abspath is undefined. So if your program has direct access http://localhost/root/app/blog.php This situation is similar, then it's best to use DirName (__file__) directly. ' /’.‘ Relative path ', or add a judgment before using Abspath (but this is a little bit of a sense of removing pants and x). ­

Note : Abspath and DirName (__file__) are used in WordPress. /’.‘ Relative paths ' combined methods, files loaded from the Web site's unified portal (root directory/index.php), using the Abspath solution (Abspath defined in the root directory/wp-config-sample.php), For those PHP files that are not accessed directly through the unified portal, WP uses DIRNAME (__FILE__). /’.‘ Relative path ' solution. ­

3. Set Apache include_path parameters

In the previous error message, there is one sentence that deserves our attention:

———————————————

Fatal error:require_once () [function.require]: Failed opening required '. /lib/smarty/functions.php ' (include_path= '.; C:/php5/pear ') in t:/study/php_rel/projects/root/app/blog.php on line 2

———————————————

The Apache include_path parameter holds the Require/include read directory, and in the error message above, the include_path contains two locations:

1) "." To load from the directory in which the current file resides

2) "C:/php5/pear" means to load from the Php5/pear directory of C disk. ­

The PHP function library provides us with the Set_include_path () function to set the include_path parameters. With the Set_include_path () function, we can customize the load location (zendframework use the Set_include_path () function to solve the path problem). ­

Let's show you how to use the Set_include_path () function:

root/index.php:

<?php

Set_include_path ('./'. Path_separator.dirname (__file__)); ­

Require_once ' app/blog.php ';

$blog = new blog ();

echo $blog->getbloginfo ();

?>

root/app/blog.php;

<?php

Require_once ' lib/smarty/functions.php ';

?>

<?php

Class Blog

Public Function Getbloginfo ()

Return ' is a test ' of blog! '; ­

?>

<?php

Echo ' root/app/blog.php loaded successed!<br/> ';

?>

Test http://localhost/root/index.php, run normally. As you can see from the root/app/blog.php, Require_once's path is more concise (no need to use Abspath or dirname (__file__)). This implementation, like the Abspath solution, requires that the system have a unified entry point (typically implemented through a. htaccess file). ­

Of course, the way to solve the PHP path more than a few, the internet has a lot of people to provide a solution for the PHP path problem, but most have a certain application of the scene, not mechanically.

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.