Working with a directory in PHP

I have pictures in my folder: 1.jpg, 2.jpg, 3.jpg…

I need to know the “max image name” so that the next one can be called max+1.

I have this: =)

$d=opendir(".");
while($name = readdir($d)){
    if($name=="." or $name=="..")
    {
        continue;
    }
    echo $name."<br>";
}

Answer 1, authority 100%

$files = glob("*.jpg");
sort($files, SORT_NUMERIC);
$max = (int)basename(array_pop($files), ".jpg");

Answer 2, authority 50%

$max = 0;
$d=opendir(".");
while($name = readdir($d)){
    if($name=="." or $name=="..")
    {
        continue;
    }
    $num = basename($name,'.jpg');
    if ($max < $num) $max=$num;
}
echo $max;