function get_next_file($file){
$maxdigits = 2; // assuming the file always ends with two digits
$dir = dirname($file) . DIRECTORY_SEPARATOR;
$fname = pathinfo($file, PATHINFO_FILENAME);
$name = substr($fname, 0, -$maxdigits);
$digits = substr($fname, -$maxdigits) + 1;
foreach(array('bmp', 'png', 'jpg', 'gif') as $ext){
$test = $dir . $name . str_pad($digits, $maxdigits, '0', STR_PAD_LEFT) . '.' . $ext;
if(file_exists($ext))return $ext;
}
return false; // no such file...
}
The complete function would look like the one above (not tested!). You would call it like so:
Assuming we have these files:
test01.png
test02.gif
test05.bmp
You get these results:
get_next_file('test01.png') => 'test02.gif'
get_next_file('test02.gif') => false // 'coz the other file is not consecutive
get_next_file('test05.bmp') => false // no other files to look for...