Page 1 of 1

opendir() trouble

Posted: Thu Jul 16, 2009 8:58 pm
by CareyEstes
I am trying to develop a simple picture gallery with PHP. I would like it to display images in a subfolder into a page in the root folder. I have gotten this code to work in a page where the .php file and the images are in the same folder(ex. pictures and .gallery.php are in the same folder.) However, I do not want pictures in my root folder, I would like to keep them in a folder titled: gallery. The problem is when I change the code from

($handle = opendir("."))

to

($handle = opendir("gallery"))

the images to do not generate inside the table I have made. Is there a different way to write the code to open in a sub-directory? Do I have to change code in the echo? I know I am missing something here. Any ideas would be great!

Here is the entire code:

Code: Select all

 
<?php
 
$columns     = 5;
$thmb_width  = 80;
$thmb_height = 80;
 
function resizeImage($originalImage,$toWidth,$toHeight){
    
    // Get the original geometry and calculate scales
    list($width, $height) = getimagesize($originalImage);
    $xscale=$width/$toWidth;
    $yscale=$height/$toHeight;
    
    // Recalculate new size with default ratio
    if ($yscale>$xscale){
        $new_width = round($width * (1/$yscale));
        $new_height = round($height * (1/$yscale));
    }
    else {
        $new_width = round($width * (1/$xscale));
        $new_height = round($height * (1/$xscale));
    }
    // Resize the original image
    $imageResized = imagecreatetruecolor($new_width, $new_height);
    $imageTmp     = imagecreatefromjpeg ($originalImage);
    imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, 
                       $new_width, $new_height, $width, $height);
 
    return $imageResized;
} 
 
function generateThumbnails(){
    global $thmb_width,$thmb_height;
    
    // Open the actual directory
    if ($handle = opendir("gallery")) {
        // Read all file from the actual directory
        while ($file = readdir($handle))  {
            // Check whether tha actual item is a valid file
            if (is_file($file)){
                // Check whether the actual image is a thumbnail
                  if (strpos($file,'_th.jpg')){
                      $isThumb = true;
                  } else {
                      $isThumb = false;
                  }
              
                  if (!$isThumb) {
                      // Process the file string
                      $dirName  = substr($file,0,strpos($file,basename($file)));
                      if (strlen($dirName) < 1) $dirName = '.';
                      $fileName = basename($file);
                      $fileMain = substr($fileName,0,strrpos($fileName,'.'));
                      $extName  = substr($fileName,strrpos($fileName,'.'),
                                          strlen($fileName)-strrpos($fileName,'.'));
                      
                      // Check if the actual file is a jpeg image
                      if (($extName == '.jpg') || ($extName == '.jpeg')){
                        $thmbFile = $dirName.'/'.$fileMain.'_th.jpg';
                        // If a thumbnail dosn't exists tahn create a new one
                        if (!file_exists($thmbFile)){
                            imagejpeg(resizeImage($file,$thmb_width,$thmb_height)
                                     ,$thmbFile,80);
                        }
                    }
                  } 
               }
           }
    }
    
}
 
function getNormalImage($file){
    $base = substr($file,0,strrpos($file,'_th.jpg'));
    if (file_exists($base.'.jpg')) return $base.'.jpg';
    elseif (file_exists($base.'.jpeg')) return $base.'.jpeg';
    else return "";
}
 
function displayPhotos(){
    global $columns;
    
    generateThumbnails();
    $act = 0;
    // Open the actual directory
    if ($handle = opendir("gallery")) {
        // Read all file from the actual directory
        while ($file = readdir($handle))  {
            // Check whether tha actual item is a valid file
            if (is_file($file)){
                // Check whether the actual image is a thumbnail
                  if (strpos($file,'_th.jpg')){
                    ++$act;
                    if ($act > $columns) {
                        echo '</tr><tr>
                           <td align="center" class="photo"><a href="'.getNormalImage($file).'">
                               <img src="'.$file.'" alt="'.$file.'"/></a></td>';    
                        $act = 1;
                    } else {
                        echo '<td align="center" class="photo"><a href="'.getNormalImage($file).'">
                           <img src="'.$file.'" alt="'.$file.'"/></a></td>';    
                    }
                      
                  }
              }
        }
    }    
}
 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "DTD/xhtml1-transitional.dtd">
<html>
<head>
   <title>Project P.A.S.S.</title>
<link href="main.css" rel="stylesheet" type="text/css" />
 
</head>
<body>
<div id="wrap">
<div id="header">
    <img src="images/Header.jpg" alt="Project P.A.S.S." width="955" height="175" /></div>
<div class="clearfix" id="main_tile">
<?php include ("Navigation.php"); ?>
<?php include ("Youth_RightColumn.php"); ?>
<div id="mainContent">
      <br />
        <center><h3>Please Work!</h3></center>
  <table align="center"><tr>     
    <?php displayPhotos(); ?>
  </table>
      <br />
      <br />
    </div>
      
  </div>
<?php include ("Footer.php"); ?>
</div>
</body>
</html>
          
 
 

Re: opendir() trouble

Posted: Fri Jul 17, 2009 5:00 am
by Jammerious
I think you are not specifying the path correctly, try checking your path string with is_dir($path) like in the php.net manual example.

Re: opendir() trouble

Posted: Fri Jul 17, 2009 4:00 pm
by CareyEstes
Thanks for the response.
I have worked on it some more, but I still can't seem to get the images back from the gallery folder to the root folder.

I changed the code to this:

Code: Select all

 
function displayPhotos(){
    global $columns;
    
    generateThumbnails();
    $act = 0;
    // Open the actual directory
    if ($handle = opendir("gallery")) {
        // Read all file from the actual directory
        while ("gallery".$file = readdir($handle))  {
            // Check whether tha actual item is a valid file
            if (is_file("gallery".$file)){
                // Check whether the actual image is a thumbnail
                  if (strpos("gallery".$file,'_th.jpg')){
                    ++$act;
                    if ($act > $columns) {
                        echo '</tr><tr>
                           <td align="center" class="photo"><a href="'.getNormalImage("gallery".$file).'">
                               <img src="'.$file.'" alt="'.$file.'"/></a></td>';    
                        $act = 1;
                    } else {
                        echo '<td align="center" class="photo"><a href="'.getNormalImage("gallery".$file).'">
                           <img src="'.$file.'" alt="'.$file.'"/></a></td>';    
                    }
                      
                  }
              }
        }
    }    
}
 
?>
 
I was thinking you would have to tell the file it was in the subfolder by putting "gallery" before every $file. That still is not the kicker. I can't seem to find the appropriate code to echo a file back from a directory.

Re: opendir() trouble

Posted: Fri Jul 17, 2009 4:52 pm
by Jammerious
Ok, this:

Code: Select all

while ("gallery".$file = readdir($handle))
Is very wrong, it is an gigantic loop, if not infinite (it sure spams your browser if you try to var_dump($file) after it).
Here's the fixed code for the iteration through the files, you can do the rest.

Code: Select all

function displayPhotos(){
    global $columns;
   
    //generateThumbnails();
    $act = 0;
    // Open the actual directory
    $dir="gallery/";
    if ($handle = opendir($dir)) {
        // Read all file from the actual directory
        while ($file = readdir($handle))  {
            // Check whether tha actual item is a valid file
            if (is_file($file)){
                 //do the magic
              }
        }
    }
    closedir($handle);
}
Compared to your first code it seems all you were missing is a slash, "gallery/" instead of "gallery", as I mentioned before...

Re: opendir() trouble

Posted: Fri Jul 17, 2009 5:50 pm
by CareyEstes
Still a no go. I have deleted the thumbnail generation function to simplify as much as I could. I used your suggestion, but I still think the problem is calling the files into the table I created in the .php file. So I tried this:

Code: Select all

 
function getNormalImage($file){
    $base = substr($file,0,strrpos($file,'_th.jpg'));
    if (file_exists($base.'.jpg')) return $base.'.jpg';
    elseif (file_exists($base.'.jpeg')) return $base.'.jpeg';
    else return "";
}
 
function displayPhotos(){
    global $columns;
    
    $act = 0;
    // Open the actual directory
    $dir="gallery/";
    if ($handle = opendir($dir)) {
        // Read all file from the actual directory
        while ($file = readdir($handle))  {
            // Check whether tha actual item is a valid file
            if (is_file($file)){
                // Check whether the actual image is a thumbnail
                  if (strpos($file,'_th.jpg')){
                    ++$act;
                    if ($act > $columns) {
                        echo '</tr><tr>
                           <td align="center" class="photo"><a href="'.getNormalImage($file).'">
                               <img src="'.$dir/$file.'" alt="'.$dir/$file.'"/></a></td>';    
                        $act = 1;
                    } else {
                        echo '<td align="center" class="photo"><a href="'.getNormalImage($file).'">
                           <img src="'.$dir/$file.'" alt="'.$dir/$file.'"/></a></td>';    
                    }
                      
                  }
              }
        }
    }    
}
 
 
but it did not work either. I also am calling the getNormalImage function for the a href. Could that be screwing everything up?

Re: opendir() trouble

Posted: Fri Jul 17, 2009 6:07 pm
by Jammerious
When troubleshooting a script you should insert echos or var_dump($var) to see what is going on.
Also, when dealing with scripts that echo html code, it is often helpful to take a look at the source code in your browser, to see what the script has outputted.
I can't run your script now but I see an obvious mistake:

Code: Select all

'<img src="'.$dir/$file.'" alt="'.$dir/$file.'"/>'
You should use $dir.$file here.
First, this: $dir/$file is a division and I can't imagine what this does to two strings :lol:
You would do this as $dir."/".$file
Second, you already got the trailing slash in $dir, so no need to do it again.
I'm off to bed, hope you manage to get it working :)

Re: opendir() trouble

Posted: Fri Jul 17, 2009 6:24 pm
by CareyEstes
That did it!!!! Thanks so much for your help. Seems it's always something simple.

:drunk: :drunk: