上午hihiyou问我,如何把“http://www.ss126.com/shop/../../bigimg/../bigimg/china-ef.com_200951117215473416.jpg”转换成实际路径的时候,我写了一个简单的方法:
$url = 'http://www.ss126.com/shop/../../bigimg/../bigimg/china-ef.com_200951117215473416.jpg';
$pathinfo = array(
'path'=>'???',//相当于项目的根目录
'url'=> 'http://www.ss126.com/'
);
$url = realpath(str_replace($pathinfo['url'],$pathinfo['path'],$url)); //获取实际路径
//然后
if(strpos($url,$pathinfo['path'])!==false){//防止路径乱掉,或者进入其他目录
$url = str_replace($pathinfo['path'],$pathinfo['info'],$url);
}else{
$url = $pathinfo['url'];
}
结果,他告诉我,不是本地程序。然后找了这样的代码 。。。
<?php
$url = "http://www.goat.com/money/dave.html";
$rel = "../images/cheese.jpg";
$com = InternetCombineURL($url,$rel);
// Returns http://www.goat.com/images/cheese.jpg
function InternetCombineUrl($absolute, $relative) {
$p = parse_url($relative);
if($p["scheme"])return $relative;
extract(parse_url($absolute));
$path = dirname($path);
if($relative{0} == '/') {
$cparts = array_filter(explode("/", $relative));
}
else {
$aparts = array_filter(explode("/", $path));
$rparts = array_filter(explode("/", $relative));
$cparts = array_merge($aparts, $rparts);
foreach($cparts as $i => $part) {
if($part == '.') {
$cparts[$i] = null;
}
if($part == '..') {
$cparts[$i - 1] = null;
$cparts[$i] = null;
}
}
$cparts = array_filter($cparts);
}
$path = implode("/", $cparts);
$url = "";
if($scheme) {
$url = "$scheme://";
}
if($user) {
$url .= "$user";
if($pass) {
$url .= ":$pass";
}
$url .= "@";
}
if($host) {
$url .= "$host/";
}
$url .= $path;
return $url;
}
?>
好吧。我承认,上面这个更方便一点。

评论 (0)
暂无评论