>>23: Brilliance... except...
$x=explode("\n",shell_exec("cd ".$dir.";ls -1 *.jpg *.jpeg *.png *.gif"));
ought to work better. ls -1
forces single-column output.
Also, this will break on Windows, but... who cares? Good thinking, my man.
Also, is sending a "Status" header really necessary?