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

后端 未结 14 2262
花落未央
花落未央 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:15

    You can use the basename() function.

    0 讨论(0)
  • 2020-11-22 11:16

    basename() has a bug when processing Asian characters like Chinese.

    I use this:

    function get_basename($filename)
    {
        return preg_replace('/^.+[\\\\\\/]/', '', $filename);
    }
    
    0 讨论(0)
  • 2020-11-22 11:16

    It's simple. For example:

    <?php
        function filePath($filePath)
        {
            $fileParts = pathinfo($filePath);
    
            if (!isset($fileParts['filename']))
            {
                $fileParts['filename'] = substr($fileParts['basename'], 0, strrpos($fileParts['basename'], '.'));
            }
            return $fileParts;
        }
    
        $filePath = filePath('/www/htdocs/index.html');
        print_r($filePath);
    ?>
    

    The output will be:

    Array
    (
        [dirname] => /www/htdocs
        [basename] => index.html
        [extension] => html
        [filename] => index
    )
    
    0 讨论(0)
  • 2020-11-22 11:18

    With SplFileInfo:

    SplFileInfo The SplFileInfo class offers a high-level object oriented interface to information for an individual file.

    Ref: http://php.net/manual/en/splfileinfo.getfilename.php

    $info = new SplFileInfo('/path/to/foo.txt');
    var_dump($info->getFilename());
    

    o/p: string(7) "foo.txt"

    0 讨论(0)
  • 2020-11-22 11:18
    <?php
    
      $windows = "F:\Program Files\SSH Communications Security\SSH Secure Shell\Output.map";
    
      /* str_replace(find, replace, string, count) */
      $unix    = str_replace("\\", "/", $windows);
    
      print_r(pathinfo($unix, PATHINFO_BASENAME));
    
    ?> 
    

    body, html, iframe { 
      width: 100% ;
      height: 100% ;
      overflow: hidden ;
    }
    <iframe src="https://ideone.com/Rfxd0P"></iframe>

    0 讨论(0)
  • 2020-11-22 11:18
    $image_path = "F:\Program Files\SSH Communications Security\SSH Secure Shell\Output.map";
    $arr = explode('\\',$image_path);
    $name = end($arr);
    
    0 讨论(0)
提交回复
热议问题