Working out the image path...partly solved, please have a look at my code
| From: | Mag | Date: | Mon, 11 Oct 2004 20:51:22 +0000 |
| Subject: | Working out the image path...partly solved, please have a look at my code | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-199236@lists.php.net to get a copy of this message | ||
Hi,
I wrote this to the list a little while back and have
been working on it...i have come 95% of the way but
for some reason cant work the other 5% out...can
somebody help please?
Basically I need to figure out the URL or a image from
a relitive path...something like this:
/blah.jpg // should return http://x.com/blah.jpg
/imgs/blah.jpg // return http://x.com/imgs/ blah.jpg
imgs/blah.jpg //return
http://x.com/t1/t2/t3/imgs/blah.jpg
../imgs/blah.jpg // etc
/../imgs/blah.jpg //etc
http://some-site-blah.com/imgs/blah.jpg //
return the
same url
running the below code will show you how far i have
come and how only 2 examples are not working for some
damn reason.
********************* code start **************
<?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>';
?>
******************* code end ***************
Thanks,
Mag
=====
------
- The faulty interface lies between the chair and the keyboard.
- Creativity is great, but plagiarism is faster!
- Smile, everyone loves a moron. :-)
_______________________________
Do you Yahoo!?
Express yourself with Y! Messenger! Free. Download now.
http://messenger.yahoo.com