find the filename from a string with php

后端 未结 3 718
面向向阳花
面向向阳花 2021-01-17 21:15
public/images/portfolio/i-vis/1.jpg

How could i remove all the path regardless of what the filename is using php?

3条回答
  •  借酒劲吻你
    2021-01-17 22:08

    Have a look at basename()

    $path = 'public/images/portfolio/i-vis/1.jpg'
    $name = basename($path); // $name == '1.jpg'
    

    Also, dirname() fetches the other part

    $dir = dirname($path); // $dir == 'public/images/portfolio/i-vis'
    

    If you need even more information - there is pathinfo()

    $info = pathinfo($path);
    var_dump($info);
    

    produces

    array(4) {
        ["dirname"]=>
        string(29) "public/images/portfolio/i-vis"
        ["basename"]=>
        string(5) "1.jpg"
        ["extension"]=>
        string(3) "jpg"
        ["filename"]=>
        string(1) "1"
    }
    

    So $info['filename'] gives you the file without the extension.

提交回复
热议问题