orange? Posted August 25, 2007 Share Posted August 25, 2007 Hi. I have been trying for ages to write a php function that will be able to be given a url as a string and then return the directory of the file in the url, so basically the whole url except the file a the end. But I can't figure out how to do it. I tried exploding the url at the slashes then putting it all back together except the last part of the array, because that would generally just be the file, but, if the url is just something like "http://www.domain.com" the last part of the array would be "www.domain.com" so that method wouldn't work. Does anyone have a function to do this or a few suggestions they could give me? Quote Link to comment Share on other sites More sharing options...
trq Posted August 25, 2007 Share Posted August 25, 2007 An example.... <?php $url = "http://servername.com/dir1/dir2/file.php"; $arr = parse_url($url); $path = dirname($arr['path']); echo $path; ?> Quote Link to comment Share on other sites More sharing options...
Fadion Posted August 25, 2007 Share Posted August 25, 2007 lol i just end up with some quick string manipulation code. Dont know nothing about the functions thorpe used, but im sure they'll work. Anyway if u want a different approach: <?php $url = "http://www.mysite.com/upload/files/images/file.jpg"; $filePos = strpos($url, strrchr($url, '/')); //find the position of the file $domain = substr($url, 7, $filePos - 7); //remove the htttp:// and file $domainParts = explode('/', $domain); foreach($domainParts as $value){ echo $value . "<br />"; //it will echo 'www.mysite.com', 'upload', 'files', 'images' } ?> Quote Link to comment Share on other sites More sharing options...
orange? Posted August 25, 2007 Author Share Posted August 25, 2007 Thanks a lot! Quote Link to comment Share on other sites More sharing options...
phpSensei Posted August 25, 2007 Share Posted August 25, 2007 lol, that took ages. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.