Use absolute path and relative path in php _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Use absolute and relative paths in php. Let's take a look at two pieces of code: Code 1: Access [php] metahttp-equivContent-Typecontenttexthtml; charsetGBK? Phpecho $ _ SERVER [DOCUMENT_ROOT];? The imgsrc..1.jpg code is as follows:

Code 1: access from a relative address

[Php]


Echo $ _ SERVER ['document _ root'];
?>


Code 2: absolute address access

[Php]


Echo $ _ SERVER ['document _ root'];
?>


Let's put the two in the first place, and let's talk about the pure html code (ps: now it's a test, so I will write a simple point, but the result can be obtained)

Code 1: relative address (test_image_1.html)

[Html]

Code 2: absolute address (test_image_2.html)
[Html] view plaincopy

(Ps: image directory: C:/Program Files/Zend/Apache2/htdocs/1.jpg
Html Directory: C:/Program Files/Zend/Apache2/htdocs/test_image /)

Note: appache is installed on the local machine, which is in the Zend fusion package.

The first method is to open the file by double-clicking the file. both test_image_1 and test_image_2 can be opened. C: /Program Files/Zend/Apache2/htdocs/test_image/test_image_1, so that all images can be displayed.

Second, access through appache using URL: 127.0.0.1/test_image/test_image_1. The result is that the relative path can be used normally and the absolute path cannot be displayed.


After checking a lot of information, we can finally draw a conclusion. Open the html file directly, because in your computer, the absolute path can be easily accessed. However, for WEB access, the default WEB root directory is htdocs/. in your computer, the directory is C:/Program Files/Zend/Apache2/htdocs/, but the WEB does not think much about it, in its understanding, I don't know c: this stuff. Therefore, the absolute path you think is the absolute path in your computer, but you do not know this path through the WEB.

The correct method should be the following two codes:

Code 1: relative address (test_image_1.html)

[Html]

There is no problem with the relative path. Therefore, we recommend that you use relative paths for ease of migration.
Code 2: absolute address (test_image_2.html)
[Html] view plaincopy

The directory here is/, so this is the absolute path.
If your image is in another location, the path on your computer is C:/Program Files/Zend/Apache2/htdocs/111/222/333/1 .jpg.

The code should be written as follows:

[Html]

In summary, 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 the html method.

Let's take a look at the PHP format given at the beginning of the article.

Code 1: access test_image_1.php with a relative address
[Php]


Echo $ _ SERVER ['document _ root'];
?>


Code 2: access test_image_2.php with an absolute address
[Php] view plaincopy


Echo $ _ SERVER ['document _ root'];
?>

First, you need to understand the PHP execution method. when you access a PHP, This PHP will be triggered to run. after the PHP is parsed by appache (to put it bluntly, php is already running, the HTML code is generated. In short, the php code on your hand is the same as html code. Php is run on the server side and then delivered to you. So I didn't fully understand it at the beginning. using php to do some actions, such as submitting a table or something, is not a matter of php, but an action of html. What you can see in your hand is the html code with the. php suffix.
Let's talk about the absolute address in PHP's eyes. of course, php knows the absolute path of your computer. for example, you need to use php to open a file and use php to read the size of an image on your computer, the absolute path can be used for resolution or something (the relative path is no problem ).

However, after PHP is executed, the results will be displayed on your webpage. for example:

[Php]

This code is written in the PHP file, but it is only the HMTL code. Therefore, it is not only about PHP, but about html, which is related to the WEB, the WEB does not know your path (the reason is already described above ). Therefore, the WEB does not know the absolute path of computers, rather than the PHP does not.

Note that the root directory of the WEB is/. However, you can access the image at the level of the root directory that it deems to be accessible. the code is as follows:

[Html]
Img src = ".../../1.jpg"/>
(The original 1.jpg file is in the same directory as the original one, while the html code is in the/image_test/Directory. Therefore, you must ../1.jpg to access the file,
Now 1.jpg is in the root directory level, so we can go back to level 1 to access it.../../1.jpg)
Author: wolinxuebin

Authorization Code 1: 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.