> /../imgs/blah.jpg should return
> http://www.textx.com/t1/t2/imgs/blah.jpg
> 
> but its returning:
> http://www.textx.com/t1/t2/t3/imgs/blah.jpg
> 
> and this: /../../imgs/blah.jpg
> should return: http://www.textx.com/t1/imgs/blah.jpg
> 
> but its returning:
> http://www.textx.com/t1/t2/imgs/blah.jpg
> 
> in both cases one extra level directory :-(
> 
> Any ideas?

ok, I gave it a try and honestly this is probably over my head :) but
here is something to try

<?php

$url='http://www.textx.com/t1/t2/t3/blah.html';

function ret_url($rel_path, $base = '')
{
        $base_path = substr($base, 0, strpos($base, '/',7));

        if(substr($rel_path,0,1)=='/' && strpos($rel_path,'/../') === false)
        {  
                return $base_path.$rel_path; 
        }

        elseif(strpos($rel_path,'://') > 0)
                {  return $rel_path; }

        else {  
                return collapse(dirname($base),$rel_path); 
        }

return $rel_path;
}

/* this function removes the /../ parts */
function collapse($path,$rel_path)
{
        
        $parsed = parse_url($path);
        preg_match_all('/\/\.\./',$rel_path, $matches); 
        if(($count = count($matches[0])) > 0) {
                $parts = preg_split('/\//', $parsed['path'], -1, PREG_SPLIT_NO_EMPTY);
                for ($i = 1;$i <= $count; $i++) {
                        array_pop($parts);
                }
                $path = str_replace($parsed['path'], '/'.implode($parts,'/'), $path);
        }

        $rel_path = (preg_match('/^\//',$rel_path)) ? $rel_path :'/'.$rel_path;
        $path = preg_replace('/\/\.\./', '', $path.$rel_path);
        
        return $path;
}


$uris = array();
$uris[] = '/blah.jpg';
$uris[] ='/imgs/blah.jpg';
$uris[] ='imgs/blah.jpg';
$uris[] ='../imgs/blah.jpg';
$uris[] ='/../imgs/blah.jpg';  // ## not working ##
$uris[] ='/../../imgs/test.jpg'; // ## not working ##
$uris[] ='http://some-site-blah.com/imgs/blah.jpg';

echo '<table border=1>';
foreach($uris as $uri)
{
echo '<tr><td>'.htmlspecialchars($uri).'</
td><td>'.htmlspecialchars(ret_url($uri,$url)).'</td></tr>';
}
echo '</table>';
?>

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to