PHP: Get last directory name from path

后端 未结 8 1877
说谎
说谎 2021-01-11 10:52

I am writing one function for getting some different database query. Now things are going well but only need to get last directory name from defined path.

$q         


        
8条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-11 11:46

    Provided answer doesn't work if your string contains the file at the end, like :

    basename('/home/mypath/test.zip');
    

    gives

    test.zip
    

    So if your string contains the file, don't forget to dirname it first

    basename(dirname('/home/mypath/test.zip'));
    

    gives

    mypath
    

提交回复
热议问题