Loop througj a directory?

I’m trying to use thie code to explore a directory


$pathtoImages = "../providers/images/".$_GET['id'];
echo $pathtoImages;
if ($dir = opendir( $pathtoImages )) {
echo "Directory Handle: $dir\
";
echo "Entries:\
";

  // loop through it, looking for any/all files:
  while (false !== ($filename = readdir( $dir ))) {
      echo "$filename\
";
      closedir( $dir );
    }
}

On
http://fixmysite.us/masterasp/admin/generate_thumbnails.php?id=2
the directory has 2 images in it
http://fixmysite.us/masterasp/providers/images/2/

What is the problem here?

Thanks…

Try moving closedir( $dir ); outside while loop.

thx

Im trying to use the code to create thumbnails for each image in the directory


$thumbWidth = 100;  
$pathtoThumbs = "../providers/images/".$_GET['id']."/thumbs/";  
$pathtoImages = "../providers/images/".$_GET['id']."/";

if ($dir = opendir( $pathtoImages )) {

  // loop through it, looking for any/all image files:
  while (false !== ($filename = readdir( $dir ))) {
    //  echo "$filename\
";
  // parse path for the extension
  $info = pathinfo($pathToImages . $filename);
  // continue only if this is an image
  if (( strtolower($info['extension']) == 'jpg' ) || ( strtolower($info['extension']) == 'png' ) || ( strtolower($info['extension']) == 'gif' ))
  {
      if(preg_match('/[.](jpg)$/', $filename)) {
      $img = imagecreatefromjpeg($pathtoImages . $filename);
  } else if (preg_match('/[.](gif)$/', $filename)) {
      $img = imagecreatefromgif($pathtoImages . $filename);
  } else if (preg_match('/[.](png)$/', $filename)) {
      $img = imagecreatefrompng($pathtoImages . $filename);
  }      echo "Creating thumbnail for {$filename} <br />";

      $width = imagesx($img);
      $height = imagesy($img);

      // calculate thumbnail size
      $new_width = $thumbWidth;
      $new_height = floor( $height * ( $thumbWidth / $width ) );

      // create a new temporary image
      $tmp_img = imagecreatetruecolor( $new_width, $new_height );

      // copy and resize old image into new image
      imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );

      // save thumbnail into a file
      imagejpeg( $tmp_img, "{$pathToThumbs}{$filename}" );
      echo '<img src="' . $pathtoThumbs . $filename . '" alt="image" />';
      echo "\
\\r";

    }
  }
      closedir( $dir );
}


But it seems like the image isnt copied, resized and moved, heres the result…
http://fixmysite.us/masterasp/admin/generate_thumbnails.php?id=2
Asyou can see by the source, the image isnt available

Why isn’t that function (imagecopyresized()) working?

What have you done in the way of testing? Functions like imagecopyresized and imagejpg return a boolean success value. You should be testing these returns, and on failure echoing the values of the params passed in. That might give you clues as to why it isn’t working. Perform these tests and tell us what you find.

thank you!