Home > php教程 > php手册 > 相对路径转绝对路径

相对路径转绝对路径

WBOY
Release: 2016-06-07 11:42:05
Original
870 people have browsed it

相对路径转绝对路径
/**<br>  * 相对路径转网络绝对路径<br>  * @param string $file<br>  * @return string<br>  */<br> function dirToHttpUrl($file) {<br> <br>     //判断文件是否存在<br>     if (!file_exists($file)) {<br>         return false;<br>     }<br> <br>     //域名<br>     $nowUrl = dirname('http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']);    //当前域名<br>     $tempUrl = explode('.', $_SERVER['HTTP_HOST']);<br>     $dirUrl = 'http://www.'.$tempUrl[1].'.'.$tempUrl[2].'/';                    //主域名<br> <br>     //文件路径的层次统计<br>     $tempFile = explode('../', $file);<br>     $tempNum = array_count_values($tempFile);<br> <br>     if (array_key_exists('', $tempNum)) {<br>         $fileNum = $tempNum[''];<br>         $fileEnd = end($tempFile);<br>     } else {<br>         $fileNum = 0;<br>         $fileEnd = '/'.substr($tempFile[0], 2);<br>     }<br> <br>     //域名层次统计<br>     $tempWeb = explode('/', $nowUrl);<br>     $tempWeb = array_slice($tempWeb, 3);<br>     $webNum = count($tempWeb);<br> <br>     //文件对应的域名<br>     if ($fileNum > $webNum) {<br>         $nowUrl = $dirUrl;<br>     }<br> <br>     //返回<br>     return $nowUrl.$fileEnd;<br> <br> }<br> <br> //dirToHttpUrl('./1.jpg');

AD:真正免费,域名+虚机+企业邮箱=0元

source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template