这篇文章主要介绍了PHP获取当前相对于域名目录的方法,可实现从URL中拆分出目录名的功能,需要的朋友可以参考下
本文实例讲述了PHP获取当前相对于域名目录的方法。分享给大家供大家参考。具体如下:
/dev/classd/index.php/download
比如这个地址,我想得到红色区域的地址用于生成站内的链接,名为 baseurl。
用 $_SERVER['SCRIPT_NAME'] 就可以了, $_SERVER['SCRIPT_NAME'] 是当前正在访问的PHP文件的相对路径,只要如下稍作处理:
从$_SERVER['SCRIPT_NAME']中获取目录部分,并且替换dirname函数可能出现的反斜杠问题
复制代码 代码如下:
$baseUrl = str_replace('\\','http://www.jb51.net/',dirname($_SERVER['SCRIPT_NAME']));
保证为空时能返回可以使用的正常值
复制代码 代码如下:
$baseUrl = empty($baseUrl) ? 'http://www.jb51.net/' : 'http://www.jb51.net/'.trim($baseUrl,'http://www.jb51.net/').'http://www.jb51.net/';
这样就可以正常获取到 /dev/classd/ 这部分的目录地址了。
顺便看了一下 Zend Framework 的 baseUrl() 函数,结果一看那么一大堆代码就没再继续看了,我就想不通同样的功能他用了一大堆一大堆的代码,从$_SERVER里获取了一堆,然后中间经过两三个类,,四五个方法才最终被我们直接调用,除了中间有三行是获取当前的PHP文件以外,其它都是没用和无谓的处理,ZF本身运行的已经够慢的了,这是什么一个意思呢。感兴趣的朋友可以研究一下。
希望本文所述对大家的php程序设计有所帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version
Recommended: Win version, supports code prompts!

SublimeText3 Chinese version
Chinese version, very easy to use

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software