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.

PHPFan
  • 4,300
  • 13
  • 57
  • 117

6 Answers6

3

Just use the basename function.

basename('http://localhost/project-folder/sub-dir-1/images/products/Moulin_1407822344.jpg');
Lorenz Meyer
  • 18,330
  • 22
  • 72
  • 114
1
$link = "http://localhost/project-folder/sub-dir-1/images/products/Moulin_1407822344.jpg";
$parts = explode('/', $link);

$filename = end($parts);
Mircea Soaica
  • 2,749
  • 1
  • 13
  • 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
  • 690
  • 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,756
  • 3
  • 18
  • 20