How do I get a file name from a full path with PHP?

后端 未结 14 2423
花落未央
花落未央 2020-11-22 10:56

For example, how do I get Output.map

from

F:\\Program Files\\SSH Communications Security\\SSH Secure Shell\\Output.map

14条回答
  •  一生所求
    2020-11-22 11:10

    To do this in the fewest lines I would suggest using the built-in DIRECTORY_SEPARATOR constant along with explode(delimiter, string) to separate the path into parts and then simply pluck off the last element in the provided array.

    Example:

    $path = 'F:\Program Files\SSH Communications Security\SSH SecureShell\Output.map'
    
    //Get filename from path
    $pathArr = explode(DIRECTORY_SEPARATOR, $path);
    $filename = end($pathArr);
    
    echo $filename;
    >> 'Output.map'
    

提交回复
热议问题