PHP simply implements relative path conversion to absolute path
函数介绍:
realpath()
函数返回绝对路径。该函数删除所有符号连接(比如 '/./', '/../' 以及多余的 '/'),返回绝对路径名。若失败,则返回 false。比如说文件不存在的话。
is_dir()
函数检查指定的文件是否是一个目录。如果目录存在,该函数返回 TRUE。
相关视频教程推荐:php视频教程
示例如下:
/** * 简单方式 start */ $rel_path = '../abc/qwe/';//相对路径 $rel_path = iconv('UTF-8', 'GBK', $rel_path);//使用iconv转换中文编码,防止乱码 if (!is_dir($rel_path)){ mkdir($rel_path,0777,true); } $abs_path = realpath($rel_path) . '/';//转换成绝对路径 echo $abs_path; echo '<hr>'; /* 删除带盘符段的路径 */ $new_abs_path = str_replace('\\','/',$abs_path); echo $new_abs_path; echo '<hr>'; $rdl_path = str_ireplace($_SERVER['PHP_SELF'],'',str_replace('\\','/',__FILE__)); echo $rdl_path; echo '<hr>'; $file_abs_url = str_replace($rdl_path,"",$new_abs_path); echo $file_abs_url; echo '<hr>'; /** * 简单方式 end */
另一种方式:
/** * 相对路径-转换->绝对路径 * @param string $RelUrl 相对路径 * @param string $PrefixUrl 前缀拼接路径 * @param string $SuffixUrl 后缀拼接路径 * @return string 返回值 */ function RelToAbs($RelUrl,$PrefixUrl = '',$SuffixUrl = ''){ $RelUrlRep = str_replace('\\','/',$RelUrl); $UrlArr = explode('/',$RelUrlRep); $NewUrlArr = array(); foreach ($UrlArr as $key=>$value){ if ($value == '..' && !empty($NewUrlArr)){ array_pop($NewUrlArr); }else if ($value != '..' && $value != '.' && $value != ''){ // && $value != '' 防止多重 斜杠(/) $NewUrlArr[] = $value; } } $UrlStr = !empty($NewUrlArr) ? implode('/',$NewUrlArr) : '/' ; return $PrefixUrl.$UrlStr.$SuffixUrl; } $file_abs_path = RelToAbs($rel_path); print_r($file_abs_path);
相关文章教程推荐:php教程
The above is the detailed content of PHP simply implements relative path conversion to absolute path. For more information, please follow other related articles on the PHP Chinese website!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

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

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

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),
