PHP의 전체 경로에서 파일 이름을 얻으려면 어떻게해야합니까?
문제
예를 들어, 어떻게 얻습니까? Output.map
~에서
F:\Program Files\SSH Communications Security\SSH Secure Shell\Output.map
PHP와 함께?
해결책
당신은 찾고 있습니다 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"
?>
다른 팁
나는 함수를 사용하여 이것을했다 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'];
그만큼 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"
와 함께 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"
파일 이름과 확장자를 얻는 방법에는 여러 가지가 있습니다. 사용하기 쉬운 다음을 사용할 수 있습니다.
$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
$filename = basename($path);
이 시도:
echo basename($_SERVER["SCRIPT_FILENAME"], '.php')
Basename ()에는 중국어와 같은 아시아 캐릭터를 처리 할 때 버그가 있습니다.
나는 이것을 사용한다 :
function get_basename($filename)
{
return preg_replace('/^.+[\\\\\\/]/', '', $filename);
}
가장 적은 줄로이 작업을 수행하려면 내장을 사용하는 것이 좋습니다. 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'
당신은 사용할 수 있습니다 베이스 이름 () 기능.
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은 저에게 효과가 없습니다. 양식 (파일)에서 파일 이름을 얻었습니다. Google Chrome (Mac OS X V10.7 (Lion))에서는 파일 변수가 다음과 같습니다.
c:\fakepath\file.txt
내가 사용할 때 :
basename($_GET['file'])
반환 :
c:\fakepath\file.txt
따라서이 경우 Sun Junwen 답변은 가장 잘 작동합니다.
Firefox에서 파일의 변수에는이 가짜 경로가 포함되어 있지 않습니다.
<?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>
간단 해. 예를 들어:
<?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
)
$image_path = "F:\Program Files\SSH Communications Security\SSH Secure Shell\Output.map";
$arr = explode('\\',$image_path);
$name = end($arr);