0

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

4 Answers 4

5

https://www.php.net/manual/en/function.basename.php

It works even on urls.

Sign up to request clarification or add additional context in comments.

Comments

2

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

2

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

0

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

1 Comment

$behind_underscore = array_pop(explode('_',basename($url))); unlink($behind_underscore);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.