Re: Working out the image path...partly solved, please have a look at my code

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



> <?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,'/../'))
>  {  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)
> {
> 
>  while(strpos($path,'/../') !== false)
>  {$p2 = ereg_replace('/([^/]*)/\.\./','/',$path);
>   if($p2==$path)
>    break;
>   $path = $p2;
>  }
> 
>  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/blah.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>';
> ?>

try this:

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;
}

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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux