For example, how do I get Output.map
例如,我如何得到Output.map。
from
从
F:\Program Files\SSH Communications Security\SSH Secure Shell\Output.map
F:\程序文件\SSH通信安全\SSH外壳安全\输出.map
with PHP?
使用PHP吗?
14 个解决方案
#1
346
You're looking for 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"
?>
#2
52
I've done this using the function PATHINFO
which creates an array with the parts of the path for you to use! For example, you can do this:
我已经使用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);
?>
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'] . '/' . $xmlSchema['basename'];
#3
10
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"
#4
7
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
#5
6
$filename = basename($path);
#6
6
basename() has a bug when processing Asian characters like Chinese.
basename()在处理像中文这样的亚洲字符时有一个错误。
I use this:
我用这个:
function get_basename($filename)
{
return preg_replace('/^.+[\\\\\\/]/', '', $filename);
}
#7
6
With SplFileInfo:
SplFileInfo:
SplFileInfo The SplFileInfo class offers a high-level object oriented interface to information for an individual file.
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:字符串(7)“foo.txt”
#8
5
Try this:
试试这个:
echo basename($_SERVER["SCRIPT_FILENAME"], '.php')
#10
2
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常量和爆炸(分隔符、字符串)将路径分隔成多个部分,然后简单地从所提供的数组中提取最后一个元素。
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'
#11
1
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, '?'));
?>
#12
1
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:
Basename不适合我。我从一个表单(文件)获得了文件名。在谷歌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上,文件的变量不包括这个fakepath。
#13
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>
#14
0
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
)
#1
346
You're looking for 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"
?>
#2
52
I've done this using the function PATHINFO
which creates an array with the parts of the path for you to use! For example, you can do this:
我已经使用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);
?>
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'] . '/' . $xmlSchema['basename'];
#3
10
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"
#4
7
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
#5
6
$filename = basename($path);
#6
6
basename() has a bug when processing Asian characters like Chinese.
basename()在处理像中文这样的亚洲字符时有一个错误。
I use this:
我用这个:
function get_basename($filename)
{
return preg_replace('/^.+[\\\\\\/]/', '', $filename);
}
#7
6
With SplFileInfo:
SplFileInfo:
SplFileInfo The SplFileInfo class offers a high-level object oriented interface to information for an individual file.
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:字符串(7)“foo.txt”
#8
5
Try this:
试试这个:
echo basename($_SERVER["SCRIPT_FILENAME"], '.php')
#9
#10
2
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常量和爆炸(分隔符、字符串)将路径分隔成多个部分,然后简单地从所提供的数组中提取最后一个元素。
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'
#11
1
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, '?'));
?>
#12
1
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:
Basename不适合我。我从一个表单(文件)获得了文件名。在谷歌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上,文件的变量不包括这个fakepath。
#13
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>
#14
0
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
)