zoukankan      html  css  js  c++  java
  • 相对路径转绝对路径

    相对路径转绝对路径

    /**
     * 相对路径转网络绝对路径
     * @param string $file
     * @return string
     */
    function dirToHttpUrl($file) {
        //判断文件是否存在
        if (!file_exists($file)) {
            return false;
        }
        //域名
        $nowUrl = dirname('http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']);    //当前域名
        $tempUrl = explode('.', $_SERVER['HTTP_HOST']);
        $dirUrl = 'http://www.'.$tempUrl[1].'.'.$tempUrl[2].'/';                    //主域名
        //文件路径的层次统计
        $tempFile = explode('../', $file);
        $tempNum = array_count_values($tempFile);
        if (array_key_exists('', $tempNum)) {
            $fileNum = $tempNum[''];
            $fileEnd = end($tempFile);
        } else {
            $fileNum = 0;
            $fileEnd = '/'.substr($tempFile[0], 2);
        }
        //域名层次统计
        $tempWeb = explode('/', $nowUrl);
        $tempWeb = array_slice($tempWeb, 3);
        $webNum = count($tempWeb);
        //文件对应的域名
        if ($fileNum > $webNum) {
            $nowUrl = $dirUrl;
        }
        //返回
        return $nowUrl.$fileEnd;
    }
    //dirToHttpUrl('./1.jpg');
  • 相关阅读:
    展示
    发布说明
    团队作业Week14
    Scrum Meeting NO.10
    Scrum Meeting NO.9
    Scrum Meeting NO.8
    Scrum Meeting NO.7
    Scrum Meeting NO.6
    ES6/ES2015核心内容
    用React & Webpack构建前端新闻网页
  • 原文地址:https://www.cnblogs.com/bluealine/p/11040807.html
Copyright © 2011-2022 走看看