On the use of absolute path and relative path in PHP _php tutorial

Source: Internet
Author: User
Tags php form
Here's a look at two pieces of code:

Code One: Relative address access

[PHP]


echo $_server[' Document_root '];
?>


Code two: Absolute address access

[PHP]


echo $_server[' Document_root '];
?>


First put these two, let us say the Pure HTML code (PS: Now is the test, then I write the abbreviated point, but can produce results)

Code One: Relative Address (test_image_1.html)

[HTML]

Code Two: Absolute Address (test_image_2.html)
[HTML] View plaincopy

(PS: Picture catalog: C:/Program files/zend/apache2/htdocs/1.jpg
HTML directory: C:/Program files/zend/apache2/htdocs/test_image/)

Note: Appache is installed on this machine and is in the Zend Fusion package.

Two ways, the first is to directly double-click Open File, Test_image_1 and test_image_2 can be opened, the URL is displayed in the C:/Program files/zend/apache2/htdocs/test_image/test _image_1, so the picture can be displayed.

The second way, through Appache access, using Url:127.0.0.1/test_image/test_image_1 access, the result is the use of relative paths can be implemented normally, the absolute path cannot be displayed.


After a lot of information, we can finally come to a conclusion. Open the HTML file directly, because under your computer, the absolute path can be easily accessed. But through the Web Access, then the Web default root directory is htdocs/, in your computer, this directory is C:/Program files/zend/apache2/htdocs/but the web does not think, in its knowledge there is no known C: this thing. So the absolute path you think is the absolute path inside your computer, but you don't know that path through the web.

The correct way should be the following two code:

Code One: Relative Address (test_image_1.html)

[HTML]

The relative path is completely fine. Therefore, it is generally recommended to use relative paths for portability.
Code Two: Absolute Address (test_image_2.html)
[HTML] View plaincopy

This is the same as the directory is/So this is the absolute path.
If you have a picture, change position, under the absolute path of your computer is C:/Program files/zend/apache2/htdocs/111/222/333/1.jpg

Then the code should be written as:

[HTML]

Do not confuse the absolute path of the computer with the absolute path of the Web server.
After the above explanation, we should understand the absolute path and relative path of HTML method.

So let's take a look at the php form given at the beginning of the article.

Code One: Relative address access test_image_1.php
[PHP]


echo $_server[' Document_root '];
?>


Code two: Absolute address access test_image_2.php
[PHP] View plaincopy


echo $_server[' Document_root '];
?>

First of all to understand the way PHP is executed, when you visit a PHP, will trigger this PHP run, PHP after Appache parsing (meaning that PHP has run out, showing some results), generated a bit of HTML code, that is to say, to your hands of PHP, In fact, HTML is the same. PHP is running on the server and then sending it to your hands. So at first I did not fully understand that with PHP to do some action, such as submitting a table or something, in fact, is not a PHP thing, but the action of HTML. In your hand, what can be shown is the HTML code that is draped with the. php suffix.
In the PHP eyes to say the absolute address, of course, PHP know the absolute path of your computer, such as you want to open a file in PHP, PHP to read the size of a picture on the computer, the resolution of what, you can completely use absolute path (relative path more no problem).

But after the execution of PHP, the results will be displayed on your Web page, such as:

[PHP]

This code, although written in PHP files, but he just HMTL code, so this is not only PHP, but the HTML is on the horse, and HTML and the Web, the Web does not know you this path (the reason above). So now is the web does not know the absolute path of the computer, and not your PHP do not know.

In a point, the root of the Web is/However, your picture is at the root of what it considers to be a level of access, and the code is as follows:

[HTML]
IMG src= ". /.. /1.jpg "/>
(The original 1.jpg is under the directory, and the HTML code is placed under/image_test/, so to: /1.jpg, to be accessed,
And now 1.jpg at the root of the level, then we will be back to a level can be accessed,. /.. /1.jpg)
Author: wolinxuebin

http://www.bkjia.com/PHPjc/478111.html www.bkjia.com true http://www.bkjia.com/PHPjc/478111.html techarticle Here are two pieces of code: Code One: Relative address access [PHP] meta HTTP-EQUIV=CONTENT-TYPECONTENT=TEXT/HTML;CHARSET=GBK? PHP echo $_server[document_ ROOT];? IMG src=. /1.jpg/code ...

  • 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.