0

Suppose, I've following URL : http://localhost/project-folder/sub-dir-1/images/products/Moulin_1407822344.jpg

Now I want only Moulin_1407822344.jpg from the above URL.

How should I get this in short, sweet and optimum way in PHP?

Can someone please help me in this regard? Thanks in advance.

PHPLover
  • 1
  • 51
  • 158
  • 311

6 Answers6

3

Just use the basename function.

basename('http://localhost/project-folder/sub-dir-1/images/products/Moulin_1407822344.jpg');
Lorenz Meyer
  • 19,166
  • 22
  • 75
  • 121
1
$link = "http://localhost/project-folder/sub-dir-1/images/products/Moulin_1407822344.jpg";
$parts = explode('/', $link);

$filename = end($parts);
Mircea Soaica
  • 2,829
  • 1
  • 14
  • 25
0

Use pathinfo php function to get the filename from URL

$fileName = pathinfo('path/products/Moulin_1407822344.jpg');

echo $fileName['basename'];

Sai Raja
  • 53
  • 4
0

Using basename() should work:

$url = "http://localhost/project-folder/sub-dir-1/images/products/Moulin_1407822344.jpg";

echo basename($url); 

Live Demo

nouseforname
  • 720
  • 1
  • 5
  • 17
0

You need basename function

echo basename("http://localhost/project-folder/sub-dir-1/images/products/Moulin_1407822344.jpg") ;
Nav
  • 23
  • 7
0
$reg = '/(?<=jpg|png|gif|jpeg).*/';
$url = 'https://example.com/2012/12/zoe.jpg?w=600&#038;h=400&#038;crop=1,"; "=""';
$rep = '';
$result = preg_replace($reg, $rep, $url);
$cleanfile = basename($result);
echo $cleanfile;

http://devcodepro.com/view/58/14/How-to-get-clean-file-image-name-with-extension-from-the-URL-in-PHP-

Peca
  • 1,892
  • 3
  • 20
  • 21