Home  >  Article  >  Backend Development  >  PHP method to get the current directory relative to the domain name_PHP tutorial

PHP method to get the current directory relative to the domain name_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 09:48:381010browse

How PHP obtains the current directory relative to the domain name

This example describes the method of PHP obtaining the current directory relative to the domain name. Share it with everyone for your reference. The details are as follows:

 http://127.0.0.1/dev/classd/index.php/download

For example, in this address, I want to get the address in the red area to generate a link within the site, named baseurl.

Just use $_SERVER['SCRIPT_NAME']. $_SERVER['SCRIPT_NAME'] is the relative path of the PHP file currently being accessed. Just do the following:

Get the directory part from $_SERVER['SCRIPT_NAME'] and replace the possible backslash problem in the dirname function

The code is as follows:

$baseUrl = str_replace('\','/',dirname($_SERVER['SCRIPT_NAME']));

It is guaranteed to return a normal value that can be used when it is empty

The code is as follows:

 $baseUrl = empty($baseUrl) ? '/' : '/'.trim($baseUrl,'/').'/';

In this way, the directory address of /dev/classd/ can be obtained normally.

By the way, I took a look at the baseUrl() function of Zend Framework. I saw such a lot of code that I stopped reading. I couldn’t figure out that it used a lot of code for the same function. A bunch of things are obtained in $_SERVER, and then after two or three classes, four or five methods are finally called directly by us. Except for the three lines in the middle that get the current PHP file, the rest are useless and meaningless processing, ZF It's already running slow enough. What does this mean? Interested friends can research it.

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1022065.htmlTechArticleHow PHP obtains the current directory relative to the domain name. This example describes the method of PHP obtaining the current directory relative to the domain name. Share it with everyone for your reference. The details are as follows: http://127.0.0.1...
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