我有一个名为图像的文件夹,我想访问文件夹中的最后一个文件,并命名诸如AB00001.png之类的图像,并命名第二个图像ImageName-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);
答案 0 :(得分:0)
按预期工作:
$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] => .
*/