简体   繁体   中英

How to access last file from folder in php

I have a folder called images I want to access the last file From Folder and the naming of the images like AB00001.png and for the second image ImageName-AB00002 so on. Whenever it reaches ImageName-AB000010, in this case, getting the last image as ImageName-AB00009, but not 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);

Works as expected:

$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] => .
 */

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM