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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.