tags:

views:

547

answers:

4

url: http://blabla.bl/blabla/ss/sd/filename How to get a filename?

+4  A: 

http://us2.php.net/manual/en/function.basename.php

It works even on urls.

Carmine Paolino
A: 

Yes it works, but last thing, sometimes basename is aaaa_somewhat. How to delete _somewhat?

matiit
$behind_underscore = array_pop(explode('_',basename($url)));unlink($behind_underscore);
thephpdeveloper
+1  A: 

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

pavium
A: 

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);
Denilson Sá