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.