PHP: Get Base (Directory) From File (URL, Path)
Posted on September 7, 2008, Filled under PHP,
Bookmark it
Thanks for visiting our website! We regularly publish posts like this one. If you are interested in receiving the latest updates as soon as they are posted, please consider subscribing to the RSS feed or to our e-mail newsletter.
If you need to determine the base element from a URL or path of a file then this snippet can help you.
<?php
/*
Source: http://www.bitrepository.com/
*/
function Get_Base_From_File($file)
{
$find = '/';
$after_find = substr(strrchr($file, $find), 1);
$strlen_str = strlen($after_find);
$result = substr($file, 0, -$strlen_str);
return $result;
}
$file = 'http://www.domain.com/path/to/file.zip';
$base = Get_Base_From_File($file);
// Output: http://www.domain.com/path/to/
echo $base;
?>
Do you wish to receive the latest updates as soon as they are posted? Get our RSS Feed or Subscribe to the Newsletter!
- September 7, 2008
- article by Gabriel C.
- 1 comment
Sponsors
Related Posts
-
PHP: Get Main Base URLat September 8, 2008 with 2 comments
-
PHP: How to calculate the size of a fileat September 11, 2008 with 2 comments
-
Free Open Source PHP MySQL Driven Knowledge Base Softwareat July 22, 2009
-
How to recursively read a directory with all its contentat September 4, 2008

One Reply to "PHP: Get Base (Directory) From File (URL, Path)"
May 26, 2009 at 3:16 AM
thanks! looking for this!