如何从PHP的完整路径获取文件名?

问题描述 投票:198回答:15

例如,我如何获得Output.map

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

用PHP?

php filenames
15个回答
409
投票

你在找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"
?>

4
投票

您可以使用basename()函数。


1
投票

要从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, '?'));
?>

1
投票

Basename对我不起作用。我从表单(文件)中获取了文件名。在Google Chrome(Mac OS X v10.7(Lion))中,文件变量变为:

c:\fakepath\file.txt

我用的时候:

basename($_GET['file'])

它返回:

c:\fakepath\file.txt

所以在这种情况下,孙俊文的答案效果最好。

在Firefox上,文件的变量不包含此伪路径。


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>

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
)

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

61
投票

我使用函数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'] . '/' . $xmlSchema['basename'];

12
投票

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"

9
投票

使用SplFileInfo:

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

参考: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”


8
投票

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

$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

7
投票
$filename = basename($path);

7
投票

试试这个:

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

7
投票

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

我用这个:

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

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'
© www.soinside.com 2019 - 2024. All rights reserved.