如何从php中的变量(url)获取文件名?

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

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

This should work for you:

<?php

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

?>

Output:

myfile.doc

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;

Alternatively explode:

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

Output:

woman4.jpg