search
HomeBackend DevelopmentPHP TutorialA brief discussion on the use of absolute paths and relative paths in php_PHP tutorial

Look at the two pieces of code below:

Code 1: Relative address access

[php]


echo $_SERVER['DOCUMENT_ROOT'];
?>
A brief discussion on the use of absolute paths and relative paths in php_PHP tutorial


Code 2: Absolute address access

[php]


echo $_SERVER['DOCUMENT_ROOT'];
?>
A brief discussion on the use of absolute paths and relative paths in php_PHP tutorial

Let鈥檚 put these two aside for a moment, and let鈥檚 talk about the pure html code (ps: now it鈥檚 a test, so I鈥檒l write it briefly, but I can get the results)

Code 1: Relative address (test_image_1.html)

[html]
A brief discussion on the use of absolute paths and relative paths in php_PHP tutorial
Code 2: Absolute address (test_image_2.html)
[html] view plaincopy
A brief discussion on the use of absolute paths and relative paths in php_PHP tutorial

(ps: Picture directory: 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, which is included in Zend鈥檚 fusion package.

Two ways, the first is to directly double-click to open the file, both test_image_1 and test_image_2 can be opened, the URL shown is C:/Program Files/Zend/Apache2/htdocs/test_image/test_image_1, so the pictures are all can be displayed.

The second method is to access through appache and use URL: 127.0.0.1/test_image/test_image_1. The result is that the relative path can be implemented normally, but the absolute path cannot be displayed.


After checking a lot of information, I can finally draw a conclusion. Open the html file directly, because under your computer, the absolute path can be easily accessed. But when accessed through WEB, the default root directory of WEB is htdocs/. In your computer, this directory is C:/Program Files/Zend/Apache2/htdocs/. However, WEB does not think so, and it does not know that there is c in its understanding. :This thing. So the absolute path you think is the absolute path in your computer, but this path is not recognized through the WEB.

The correct way should be the following two codes:

Code 1: Relative address (test_image_1.html)

[html]
A brief discussion on the use of absolute paths and relative paths in php_PHP tutorial
Relative paths are absolutely fine. Therefore, it is generally recommended to use relative paths to facilitate transplantation.
Code 2: Absolute address (test_image_2.html)
[html] view plaincopy
A brief discussion on the use of absolute paths and relative paths in php_PHP tutorial
The following directory here is /, so this is the absolute path.
If you change the location of your picture, the absolute path on your computer is C:/Program Files/Zend/Apache2/htdocs/111/222/333/1.jpg

Then the code should be written as:

[html]
A brief discussion on the use of absolute paths and relative paths in php_PHP tutorial
In summary, don鈥檛 confuse the absolute path of the computer with the absolute path of the WEB server.
After the above explanation, you should understand the absolute path and relative path in html mode.

So let鈥檚 take a look at the form of PHP given at the beginning of the article.

Code 1: Relative address access test_image_1.php
[php]


echo $_SERVER['DOCUMENT_ROOT'];
?>
A brief discussion on the use of absolute paths and relative paths in php_PHP tutorial


Code 2: Absolute address access test_image_2.php
[php] view plaincopy


echo $_SERVER['DOCUMENT_ROOT'];
?>
A brief discussion on the use of absolute paths and relative paths in php_PHP tutorial
First of all, you need to understand the execution method of PHP. When you access a PHP, it will trigger the PHP to run. After PHP is parsed by appache (the implication is that PHP has finished running and some results are displayed), some HTML code is generated, and To put it simply, the php you have is actually the same as html. PHP is run on the server side and then delivered to you. So I didn't fully understand it at first. Using PHP to do some actions, such as submitting forms, is actually not a PHP thing, but an HTML action. What comes into your hands and can be displayed is the html code with the .php suffix.
Let鈥檚 talk about the absolute address in the eyes of PHP. Of course, PHP knows the absolute path of your computer. For example, if you want to use PHP to open a file and use PHP to read the size and resolution of an image on your computer, it is completely fine. Use absolute paths (relative paths are less problematic).

But after PHP is executed, the results will be displayed on your web page, so for example:

[php]
A brief discussion on the use of absolute paths and relative paths in php_PHP tutorial
Although this code is written in the PHP file, it is only HMTL code, so it is not just about PHP at this time, but HTML is launched, and HTML is related to WEB, and WEB does not recognize your path (the reason has been mentioned above ). So now it's the WEB that doesn't know the absolute path of the computer, not your PHP that doesn't know it.

One thing to mention, the root directory of the WEB is /. However, your pictures are also accessible one level above the root directory it considers. The code is as follows:

[html]
img />
(Originally, 1.jpg is in the following directory, and the html code is placed under /image_test/, so you need ../1.jpg to access it,
Now 1.jpg is one level above the root directory, then we can access it by going back one level, ../../1.jpg)
Author: wolinxuebin

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478111.htmlTechArticleLook at the two pieces of code below: Code 1: Relative address access [php] meta http-equiv=Content-Typecontent= text/html;charset=GBK ?php echo $_SERVER[DOCUMENT_ROOT]; ? img src=../1.jpg / Code...
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How does PHP identify a user's session?How does PHP identify a user's session?May 01, 2025 am 12:23 AM

PHPidentifiesauser'ssessionusingsessioncookiesandsessionIDs.1)Whensession_start()iscalled,PHPgeneratesauniquesessionIDstoredinacookienamedPHPSESSIDontheuser'sbrowser.2)ThisIDallowsPHPtoretrievesessiondatafromtheserver.

What are some best practices for securing PHP sessions?What are some best practices for securing PHP sessions?May 01, 2025 am 12:22 AM

The security of PHP sessions can be achieved through the following measures: 1. Use session_regenerate_id() to regenerate the session ID when the user logs in or is an important operation. 2. Encrypt the transmission session ID through the HTTPS protocol. 3. Use session_save_path() to specify the secure directory to store session data and set permissions correctly.

Where are PHP session files stored by default?Where are PHP session files stored by default?May 01, 2025 am 12:15 AM

PHPsessionfilesarestoredinthedirectoryspecifiedbysession.save_path,typically/tmponUnix-likesystemsorC:\Windows\TemponWindows.Tocustomizethis:1)Usesession_save_path()tosetacustomdirectory,ensuringit'swritable;2)Verifythecustomdirectoryexistsandiswrita

How do you retrieve data from a PHP session?How do you retrieve data from a PHP session?May 01, 2025 am 12:11 AM

ToretrievedatafromaPHPsession,startthesessionwithsession_start()andaccessvariablesinthe$_SESSIONarray.Forexample:1)Startthesession:session_start().2)Retrievedata:$username=$_SESSION['username'];echo"Welcome,".$username;.Sessionsareserver-si

How can you use sessions to implement a shopping cart?How can you use sessions to implement a shopping cart?May 01, 2025 am 12:10 AM

The steps to build an efficient shopping cart system using sessions include: 1) Understand the definition and function of the session. The session is a server-side storage mechanism used to maintain user status across requests; 2) Implement basic session management, such as adding products to the shopping cart; 3) Expand to advanced usage, supporting product quantity management and deletion; 4) Optimize performance and security, by persisting session data and using secure session identifiers.

How do you create and use an interface in PHP?How do you create and use an interface in PHP?Apr 30, 2025 pm 03:40 PM

The article explains how to create, implement, and use interfaces in PHP, focusing on their benefits for code organization and maintainability.

What is the difference between crypt() and password_hash()?What is the difference between crypt() and password_hash()?Apr 30, 2025 pm 03:39 PM

The article discusses the differences between crypt() and password_hash() in PHP for password hashing, focusing on their implementation, security, and suitability for modern web applications.

How can you prevent Cross-Site Scripting (XSS) in PHP?How can you prevent Cross-Site Scripting (XSS) in PHP?Apr 30, 2025 pm 03:38 PM

Article discusses preventing Cross-Site Scripting (XSS) in PHP through input validation, output encoding, and using tools like OWASP ESAPI and HTML Purifier.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment