我試圖找出這個函式和 getBasename (https://php.dev.org.tw/manual/splfileinfo.getbasename.php) 之間的差異,而我唯一能看到的差異是檔案位於檔案系統根目錄下,且根目錄有被指定的情況。
<?php
函式 getInfo($reference)
{
$file = new SplFileInfo($reference);
var_dump($file->getFilename());
var_dump($file->getBasename());
}
$test = [
'/path/to/file.txt',
'/path/to/file',
'/path/to/',
'path/to/file.txt',
'path/to/file',
'file.txt',
'/file.txt',
'/file',
];
foreach ($test as $file) {
getInfo($file);
}
// 將會回傳:
/*
string(8) "file.txt"
string(8) "file.txt"
string(4) "file"
string(4) "file"
string(2) "to"
string(2) "to"
string(8) "file.txt"
string(8) "file.txt"
string(4) "file"
string(4) "file"
string(8) "file.txt"
string(8) "file.txt"
string(9) "/file.txt" // 注意 getFilename 包含了 '/'
string(8) "file.txt" // 但 getBasename 則沒有
string(5) "/file" // getFilename 同上
string(4) "file" // getBasename 同上
*/
?>