263

例如,我如何获得Output.map

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

用PHP?

4

14 回答 14

502

你正在寻找basename.

PHP手册中的示例:

<?php
$path = "/home/httpd/html/index.php";
$file = basename($path);         // $file is set to "index.php"
$file = basename($path, ".php"); // $file is set to "index"
?>
于 2009-09-13T16:51:15.403 回答
80

我已经使用该函数完成了此操作,该函数PATHINFO创建了一个包含路径部分的数组供您使用!例如,您可以这样做:

<?php
    $xmlFile = pathinfo('/usr/admin/config/test.xml');

    function filePathParts($arg1) {
        echo $arg1['dirname'], "\n";
        echo $arg1['basename'], "\n";
        echo $arg1['extension'], "\n";
        echo $arg1['filename'], "\n";
    }

    filePathParts($xmlFile);
?>

这将返回:

/usr/admin/config
test.xml
xml
test

从 PHP 5.2.0 开始就可以使用这个函数了!

然后,您可以根据需要操作所有部分。例如,要使用完整路径,您可以这样做:

$fullPath = $xmlFile['dirname'] . '/' . $xmlFile['basename'];
于 2012-02-24T15:13:24.383 回答
16

有几种方法可以获取文件名和扩展名。您可以使用以下易于使用的。

$url = 'http://www.nepaltraveldoor.com/images/trekking/nepal/annapurna-region/Annapurna-region-trekking.jpg';
$file = file_get_contents($url); // To get file
$name = basename($url); // To get file name
$ext = pathinfo($url, PATHINFO_EXTENSION); // To get extension
$name2 =pathinfo($url, PATHINFO_FILENAME); // File name without extension
于 2015-06-23T06:06:56.300 回答
14

使用SplFileInfo

SplFileInfo SplFileInfo 类为单个文件的信息提供了一个面向对象的高级接口。

参考: http: //php.net/manual/en/splfileinfo.getfilename.php

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

o / p:字符串(7)“foo.txt”

于 2013-08-22T11:54:25.500 回答
13

basename功能应该给你你想要的:

给定一个包含文件路径的字符串,此函数将返回文件的基本名称。

例如,引用手册页:

<?php
    $path = "/home/httpd/html/index.php";
    $file = basename($path);         // $file is set to "index.php"
    $file = basename($path, ".php"); // $file is set to "index"
?>

或者,在您的情况下:

$full = 'F:\Program Files\SSH Communications Security\SSH Secure Shell\Output.map';
var_dump(basename($full));

你会得到:

string(10) "Output.map"
于 2009-09-13T16:51:35.593 回答
11

尝试这个:

echo basename($_SERVER["SCRIPT_FILENAME"], '.php') 
于 2013-11-28T00:38:29.617 回答
9

basename() 在处理像中文这样的亚洲字符时有一个错误。

我用这个:

function get_basename($filename)
{
    return preg_replace('/^.+[\\\\\\/]/', '', $filename);
}
于 2011-12-02T10:32:33.173 回答
8
$filename = basename($path);
于 2009-09-13T16:51:23.207 回答
5

您可以使用basename()函数。

于 2009-09-13T16:51:44.207 回答
4

为了在最少的行中做到这一点,我建议使用内置DIRECTORY_SEPARATOR常量以及explode(delimiter, string)将路径分成几部分,然后简单地取出提供的数组中的最后一个元素。

例子:

$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'
于 2015-08-17T16:40:07.663 回答
3

要从 URI 中获取确切的文件名,我将使用以下方法:

<?php
    $file1 =basename("http://localhost/eFEIS/agency_application_form.php?formid=1&task=edit") ;

    //basename($_SERVER['REQUEST_URI']); // Or use this to get the URI dynamically.

    echo $basename = substr($file1, 0, strpos($file1, '?'));
?>
于 2011-11-20T01:51:12.070 回答
1
$image_path = "F:\Program Files\SSH Communications Security\SSH Secure Shell\Output.map";
$arr = explode('\\',$image_path);
$name = end($arr);
于 2019-04-04T09:45:11.157 回答
0

这很简单。例如:

<?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);
?>

输出将是:

Array
(
    [dirname] => /www/htdocs
    [basename] => index.html
    [extension] => html
    [filename] => index
)
于 2014-01-24T14:43:12.307 回答
0
<?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>

于 2017-09-23T14:56:55.880 回答