1

That is not about getting file name from the url of current page. I have a php file like that.

<?php
$fileurl = 'http://example.com/filepath/filepath2/myfile.doc';
?>

here, The page's url doesn't matter. I need to extract filename from $fileurl. All I need to get myfile.doc from $fileurl to Something like echo $filename;

Output: myfile.doc

Sahriar Saikat
  • 613
  • 2
  • 7
  • 17

4 Answers4

5

Use basename() function in php to return a file name from the path.Use the code below

<?php

    $fileurl = 'http://example.com/filepath/filepath2/myfile.doc';
    $file_name=basename($fileurl);
echo $file_name; // Will output myfile.doc

?>

Hope this helps you

Utkarsh Dixit
  • 4,267
  • 3
  • 15
  • 38
1

This should work for you:

<?php

    $fileurl = 'http://example.com/filepath/filepath2/myfile.doc';
    echo basename($fileurl);

?>

Output:

myfile.doc
Rizier123
  • 58,877
  • 16
  • 101
  • 156
0

You can also use string functions like strrpos to get the last position of '/' and then use the substr and strlen functions to get the last part of the string.

$fileurl = 'http://example.com/filepath/filepath2/myfile.doc'; $file_name=substr($fileurl,strrpos($fileurl,'/')+1,strlen($fileurl));

echo $file_name;
0

Alternatively explode:

<?php
echo end(explode('/','http://localhost/login/uploads/blog_images/woman4.jpg'));

Output:

woman4.jpg
Progrock
  • 7,373
  • 1
  • 19
  • 25