PHP: Get Base (Directory) From File (URL, Path)
Posted on September 7, 2008, under PHP,
Bookmark it
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.
- 2 comments
Related Posts
-
PHP: Get Main Base URLat September 8, 2008 with 5 comments
-
PHP: How to calculate the size of a fileat September 11, 2008 with 2 comments
-
jQuery File Upload Plugin: non-Flash Extensible File Uploaderat March 21, 2011
-
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

2 Replies to "PHP: Get Base (Directory) From File (URL, Path)"
May 26, 2009 at 3:16 AM
thanks! looking for this!
April 30, 2010 at 2:27 PM
it doesn’t work if the base URL doesnt have trailing slash,
like http://domain.com
returns htp://