简体   繁体   English

如何使用 PHP 从完整路径中获取文件名?

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

For example, how do I get Output.map例如,我如何获得Output.map

from

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

with PHP?用PHP?

You're looking for basename .您正在寻找basename

The example from the PHP manual: 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"
?>

I've done this using the function PATHINFO which creates an array with the parts of the path for you to use!我已经使用PATHINFO函数完成了此操作,该函数创建了一个包含路径部分的数组供您使用! For example, you can do this:例如,您可以这样做:

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

This will return:这将返回:

/usr/admin/config
test.xml
xml
test

The use of this function has been available since PHP 5.2.0!从PHP 5.2.0开始就可以使用这个功能了!

Then you can manipulate all the parts as you need.然后您可以根据需要操作所有部件。 For example, to use the full path, you can do this:例如,要使用完整路径,您可以这样做:

$fullPath = $xmlFile['dirname'] . '/' . $xmlFile['basename'];

There are several ways to get the file name and extension.有多种方法可以获取文件名和扩展名。 You can use the following one which is easy to use.您可以使用以下易于使用的方法。

$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

With SplFileInfo :使用SplFileInfo

SplFileInfo The SplFileInfo class offers a high-level object oriented interface to information for an individual file. SplFileInfo SplFileInfo 类为单个文件的信息提供了一个面向对象的高级接口。

Ref : http://php.net/manual/en/splfileinfo.getfilename.php参考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" o/p: string(7) "foo.txt"

The basename function should give you what you want: basename函数应该给你你想要的:

Given a string containing a path to a file, this function will return the base name of the file.给定一个包含文件路径的字符串,此函数将返回文件的基本名称。

For instance, quoting the manual's page:例如,引用手册页:

<?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"
?>

Or, in your case:或者,就您而言:

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

You'll get:你会得到:

string(10) "Output.map"

试试这个:

echo basename($_SERVER["SCRIPT_FILENAME"], '.php') 

basename() has a bug when processing Asian characters like Chinese. basename() 在处理像中文这样的亚洲字符时有一个错误。

I use this:我用这个:

function get_basename($filename)
{
    return preg_replace('/^.+[\\\\\\/]/', '', $filename);
}
$filename = basename($path);

您可以使用basename()函数。

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.要在最少的行中做到这一点,我建议使用内置的DIRECTORY_SEPARATOR常量和explode(delimiter, string)将路径分成几部分,然后简单地删除提供的数组中的最后一个元素。

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'

To get the exact file name from the URI, I would use this method:要从 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, '?'));
?>

Basename does not work for me. 基本名称不适用于我。 I got the filename from a form (file). 我从表单(文件)获得文件名。 In Google Chrome (Mac OS X v10.7 (Lion)) the file variable becomes: 在Google Chrome(Mac OS X v10.7(Lion))中,文件变量变为:

c:\fakepath\file.txt

When I use: 当我使用时:

basename($_GET['file'])

it returns: 它返回:

c:\fakepath\file.txt

So in this case the Sun Junwen answer works best. 因此,在这种情况下,孙俊文的答案最有效。

On Firefox the file's variable does not include this fakepath. 在Firefox上,文件的变量不包含此falsepath。

$image_path = "F:\Program Files\SSH Communications Security\SSH Secure Shell\Output.map";
$arr = explode('\\',$image_path);
$name = end($arr);

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM