url: http://blabla.bl/blabla/ss/sd/filename How to get a filename?
4 Answers
Use earcar's suggestion (basename) to get the filename.
BUT, if we're starting with a URL and the filename includes a query string, use Mauris's suggestion as well.
The query string will start with ? (that's how we know it's not part of the filename) and we can use
explode('?', basename($url));
This is summed up by the online PHP manual for basename
Comments
Given an arbitrary URL, I think you should use basename() together with parse_url(). Something like this:
$parsed_url = parse_url($url);
$path = $parsed_url['path'];
$filename = basename($path);
Comments
Yes it works, but last thing, sometimes basename is aaaa_somewhat. How to delete _somewhat?
1 Comment
mauris
$behind_underscore = array_pop(explode('_',basename($url))); unlink($behind_underscore);