How to Get filename from a variable (url) in php?

前端 未结 4 850
孤城傲影
孤城傲影 2021-01-16 08:47

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



        
相关标签:
4条回答
  • 2021-01-16 09:24

    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 讨论(0)
  • 2021-01-16 09:44

    This should work for you:

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

    Output:

    myfile.doc
    
    0 讨论(0)
  • 2021-01-16 09:46

    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

    0 讨论(0)
  • 2021-01-16 09:46

    Alternatively explode:

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

    Output:

    woman4.jpg
    
    0 讨论(0)
提交回复
热议问题