Jump to content

[SOLVED] Getting Directory of Url


orange?

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/66594-solved-getting-directory-of-url/
Share on other sites

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'
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.