我有一个名为"图像"的文件夹,我想从文件夹中访问最后一个文件以及诸如ab00001.png之类的图像的命名,以及第二个图像ImageMename-ab00002。每当到达ImageName-ab000010时,在这种情况下,将最后一个图像作为Imagename-AB00009,而不是ImageName-AB000010。
// scanning last file
$files = scandir('../images/', SCANDIR_SORT_DESCENDING);
//for name of last file
echo $newest_file = $files[0];
// for substring after hyphen
$lclStrAfterHyphen = substr($newest_file, strpos($newest_file, "-") + 1);
// checking string length
echo strlen($lclStrAfterHyphen);
正常工作:
$files = scandir(__DIR__.'/files', SCANDIR_SORT_DESCENDING);
print_r($files);
/*
* OUTPUT:
* Array
* (
* [0] => testfile010.txt
* [1] => testfile009.txt
* [2] => testfile008.txt
* [3] => testfile007.txt
* [4] => testfile006.txt
* [5] => testfile005.txt
* [6] => testfile004.txt
* [7] => testfile003.txt
* [8] => testfile002.txt
* [9] => testfile001.txt
* [10] => ..
* [11] => .
*/