When I run it, it appears to be an error with opendir(). I don't know why, but it's only letting me pass relative directories. Do you get an opendir() error?
Also, I don't think my loop code should matter, but that one might be a little faster.
Well, since you're working with a file, that makes it a little easier.
Simply open the file for reading:
$file = fopen("file.name","rb"); // you can erase the "b" if you're on *nix
Then retrieve the data from the file:
$data = fread($file,filesize("file.name"));
Close your file:
fclose($file);
Now, you have a variable that contains all of the code, all on one line. We want to split that into multiple lines (an array, if you will) and loop through those:
$dataary = split("\n",$data);
for ($i=0;$i<count($dataary);$i++) {
// use image stuff from the last one here
// The filename is $dataary[$i]
}