[Solved] PHP: How to generate indexed name with string in file output?


Your want to fix all your current images to the correct format (See @ Félix Gagnon-Grenier answer), then once you done that you can do something like the following:

//get array of current images
$imgs = glob(UPLOAD_DIR.'*.png');

//select last image in array, strip out all non-alpha's then pad it with 4 0's
$next = str_pad(preg_replace("/[^0-9]/","", end($imgs))+1, 4, "0", STR_PAD_LEFT);

$file = UPLOAD_DIR.'picture_'.$next.'.png';

Edit

See comments – file based counter

$count_file = UPLOAD_DIR.'count.txt'; //or put somewhere else

//make count file if not exists
if(!file_exists($count_file)){
    file_put_contents($count_file,0);
}

//get last image count + 1 
$next = file_get_contents($count_file)+1;

//set file var 
$file = UPLOAD_DIR.'picture_'.sprintf("%04s",$next).'.png';

//update counter
file_put_contents($count_file, $next);

10

solved PHP: How to generate indexed name with string in file output?