您好,欢迎访问一九零五行业门户网

相对路径 转 网络绝对路径(支持二级域名) ./a.jpg => http://www.w.com/a.jpg

昨天在写用迅雷下载时,发现 ./a.jpg的文件转成迅雷不是网络地址,然后写了一个方法转换。目前处在php基础层次,有不足还望请教。
会自动判断当前的域名与主域名,以下是形式:
./a.jpg => http://about.w.com/a.jpg;
../a.jpg => http://www.w.com/a.jpg /** * 相对路径转网络绝对路径 * @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');
复制代码
其它类似信息

推荐信息