hi!
i started to play with some kind of image gallery and gd libraries but i ran on unsolvable problem (at least for me 🙂)
so, i found this code:
http://www.tutorialized.com/tutorial/Creating-a-simple-photo-gallery/24828
and i tried to customize it for my purpose...
everything works fine when i'm using

if ($handle = opendir("."))
...

but because i don't want to have php file in same directory where jpgs are i change "." part to $dir variable but then everything goes to hell!

if ($handle = opendir($dir))
...

in some way i'm sure that $dir variable IS directory because when i do this:

var_dump(is_dir($dir));

return is

bool(true)

i'm searching the web and php.org for hours but there's nothing that i could consider as help so please is there any tip or thing that i should be aware of?
thank you in advance!

    How exactly does it go to hell?
    What errors are you getting?

      well, that is the problem, i don't get ANY error! it just won't work.... when i use displayPhotos() function at the end of the code it does NOTHING.... blank page! 🙁 i can send you more code if you need it....

        oh, sorry for delay... well basicly this is my code:

        $dir = "image/galerie/ups/".$_GET['id'];
        
        $columns     = 3;
        $thmb_width  = 100;
        $thmb_height = 100;
        
        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($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')){
                          $isThumb = true;
                      } else {
                          $isThumb = false;
                      }
        
                      if (!$isThumb) {
                          // Process the file string
                          $dirName  = substr($file,0,strpos($file,basename($file)));
                          if (strlen($dirName) < 1) $dirName = $dir;
                          $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($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 class="photo"><a href="'.getNormalImage($file).'">
                                   <img src="'.$file.'" alt="'.$file.'"/></a></td>';    
                            $act = 1;
                        } else {
                            echo '<td class="photo"><a href="'.getNormalImage($file).'">
                               <img src="'.$file.'" alt="'.$file.'"/></a></td>';    
                        }
        
                      }
                  }
            }
        }    
        }
        

        i made $dir variable because i have to manipulate with lots of directories and $_GET['id'] defines that specific working directory.... all this code works perfectly when i use opendir('.') instead of opendir($dir) but than i have to put php file in same directory where my images are and that complicates a lot with my CMS coding... i wonder could it be a problem with defining global variables since globals are OFF in my php.ini......?

          Have you tested the script using static filenames (as opposed to variables that change).

            if you mean that i put opendir("image/galerie/ups/1") or opendir("image/galerie/ups/".$_GET['id']) yes, i did and i get the same result --> NOTHING 🙂 nothing works except opendir(".") and i really don't understand why... 🙁 do you know is there any alternative to what i'm doing? ohh, i hate when php makes me dumb like this 🙂

              I'm clueless too 🙁 Maybe someone better will come along to help.

                I believe the problem is actually rather simple: $dir is global relative to the generateThumbnails() and displayPhotos() functions, but was not declared as such in them. The solution is to pass $dir as an argument to those functions that need to read from the directory.

                  20 days later

                  damnations! 🙂
                  i've tried everything but obviously i don't have luck with that...
                  i must admit that i don't know actually how to handle functions so i've tried to put $dir in these functions (and changed back $handle = opendir($dir) to $handle = opendir("."))

                  function generateThumbnails($dir){
                  global $thmb_width,$thmb_height;
                  
                  // Open the actual directory
                  if ($handle = opendir(".")) {
                  ...

                  and

                  function displayPhotos($dir){
                  global $columns;
                  
                  generateThumbnails($dir);
                  $act = 0;
                  // Open the actual directory
                  if ($handle = opendir(".")) {
                  ...

                  (lines 32 and 80 from code above)
                  ...but still nothing.... images are not displayed at all!
                  please, any help would be really appreciated 🙂
                  thanks!

                    hmmm...
                    i'm trying to debug my code and i found out one 'interesting' thing:
                    my script goes to hell because JPG files in my directory are NOT FILES???
                    for example, the script

                    function generateThumbnails(){
                    global $thmb_width,$thmb_height,$dir;
                    
                    echo "<p><b>dir</b>: ".$dir;
                    
                    if ($handle = opendir($dir)) {
                    
                    while (false !== ($file = readdir($handle))) {
                    
                    echo "<p><b>file:</b> ".$file." - <b>var_dump</b>: ";
                        var_dump(is_file($file)) . "\n";
                    
                    if (is_file($file)){
                    
                    echo "<p><b>IS FILE!!!</b>";
                    
                    if (strpos($file,'_th.jpg')){
                    $isThumb = true;
                    } else {
                    $isThumb = false;
                    }
                    
                    if (!$isThumb) {
                    
                    $dirName  = substr($file,0,strpos($file,basename($file)));
                    if (strlen($dirName) < 1) $dirName = $dir;
                    $fileName = basename($file);
                    $fileMain = substr($fileName,0,strrpos($fileName,'.'));
                    $extName  = substr($fileName,strrpos($fileName,'.'),
                    strlen($fileName)-strrpos($fileName,'.'));
                    
                    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);
                    }
                    }
                    } 
                    }
                    }
                    }  
                    }

                    would output

                    dir: image/galerije/ups/5/
                    
                    file: IMG_2900.jpg - var_dump: bool(false)
                    
                    file: IMG_2973.JPG - var_dump: bool(false)
                    
                    file: .. - var_dump: bool(false)
                    
                    file: IMG_2922.JPG - var_dump: bool(false)
                    
                    file: IMG_2954.JPG - var_dump: bool(false)
                    
                    file: IMG_2986.JPG - var_dump: bool(false)
                    
                    file: IMG_2901.JPG - var_dump: bool(false)
                    
                    file: . - var_dump: bool(false)
                    
                    file: IMG_2968.JPG - var_dump: bool(false)
                    
                    file: IMG_2921.JPG - var_dump: bool(false)
                    
                    file: IMG_2923.JPG - var_dump: bool(false) 

                    and it stops here....

                    when i put for example PHP file inside that directory my script says bool(true) for that file...

                    well, NOW i'm 😕 ! 🙁 damnations!!!!

                      Stop for a moment. Write a short and simple script that reads a directory and places the names of the .jpg files within that directory in an array, and returns this array. The function is of the form:

                      function listJpegFiles($dir_path) {
                          $filenames = array();
                          // ...
                          return $filenames;
                      }

                      It would be used such as:

                      $dir_path = "image/galerie/ups/id";
                      $jpeg_files = listJpegFiles($dir_path);
                      foreach ($jpeg_files as $jpeg_file) {
                          echo $jpeg_file . "<br />\n";
                      }

                        @
                        thank you very much for the effort, i hope that this 'test' will tell you something about how my php works 🙂
                        so what i did is:

                        $dir = "image/galerije/ups/".$_GET['id']."/";
                        
                        echo "dir: " . $dir . "<br />\n";
                        
                        function listJpegFiles($dir) {
                            $filenames = array();
                            return $filenames;
                        }
                        
                        $jpeg_files = listJpegFiles($dir); 
                        foreach ($jpeg_files as $jpeg_file) {
                            echo "filename: " . $jpeg_file . "<br />\n";
                        }
                        

                        and in return i got only first echo (dir: image/galerije/ups/5/) and after that blank :-/
                        is my code what you wanted me to do? did i miss something?
                        i really don't understand.... shouldn't that work perfectly??? is there any explanation for all this? i would really like to understand 🙂
                        i don't know..... i'm going to put my head deep in pillow and cry loudly! :queasy:

                          oh, after many unsuccessful debuggings i returned to beloved php.net and i found a simple code for opening directories and checking files in directory:
                          the code is:

                          $dir = "/etc/php5/";
                          
                          // Open a known directory, and proceed to read its contents
                          if (is_dir($dir)) {
                              if ($dh = opendir($dir)) {
                                  while (($file = readdir($dh)) !== false) {
                                      echo "filename: $file : filetype: " . filetype($dir . $file) . "\n";
                                  }
                                  closedir($dh);
                              }
                          }

                          what i did is this:

                          $dir = "image/galerije/ups/".$_GET[id]."/";
                          
                          if (is_dir($dir)) {
                              if ($dh = opendir($dir)) {
                                  while (($file = readdir($dh)) !== false) {
                                      echo "filename: $file : filetype: " . filetype($dir . $file) . "<br />\n";
                                  }
                                  closedir($dh);
                              }
                          }
                          
                          echo "##########################################################";
                          
                          function generateThumbnails(){
                              global $thmb_width,$thmb_height,$dir;
                          
                          // Open the actual directory
                          if ($handle = opendir($dir)) {
                          
                              // Read all file from the actual directory
                              while (($file = readdir($handle)) !== false) {
                                        echo "<p>file: ".$file." - var_dump: ";
                                        var_dump(is_file($file));
                                  // Check whether the actual item is a valid file
                                  if (is_file($file)){
                                     echo ("$file is a regular file");
                                     }
                                     else
                                     { 
                                     echo ("$file is not a regular file");
                                     }
                               }
                           }
                          }

                          the output is:

                          filename: IMG_2901.jpeg : filetype: file
                          filename: IMG_2900.jpg : filetype: file
                          filename: IMG_2973.JPG : filetype: file
                          filename: .. : filetype: dir
                          filename: IMG_2922.JPG : filetype: file
                          filename: galerije.php : filetype: file
                          filename: IMG_2954.JPG : filetype: file
                          filename: IMG_2986.JPG : filetype: file
                          filename: . : filetype: dir
                          filename: IMG_2968.JPG : filetype: file
                          filename: IMG_2921.JPG : filetype: file
                          filename: test.jpg : filetype: file
                          filename: IMG_2923.JPG : filetype: file
                          ##########################################################
                          
                          file: IMG_2901.jpeg - var_dump: bool(false) IMG_2901.jpeg is not a regular file
                          file: IMG_2900.jpg - var_dump: bool(false) IMG_2900.jpg is not a regular file
                          file: IMG_2973.JPG - var_dump: bool(false) IMG_2973.JPG is not a regular file
                          file: .. - var_dump: bool(false) .. is not a regular file
                          file: IMG_2922.JPG - var_dump: bool(false) IMG_2922.JPG is not a regular file
                          file: galerije.php - var_dump: bool(true) galerije.php is a regular file
                          file: IMG_2954.JPG - var_dump: bool(false) IMG_2954.JPG is not a regular file
                          file: IMG_2986.JPG - var_dump: bool(false) IMG_2986.JPG is not a regular file
                          file: . - var_dump: bool(false) . is not a regular file
                          file: IMG_2968.JPG - var_dump: bool(false) IMG_2968.JPG is not a regular file
                          file: IMG_2921.JPG - var_dump: bool(false) IMG_2921.JPG is not a regular file
                          file: test.jpg - var_dump: bool(false) test.jpg is not a regular file
                          file: IMG_2923.JPG - var_dump: bool(false) IMG_2923.JPG is not a regular file

                          NOTICE galerije.php IN SECOND LISTING!
                          i really don't get it?
                          especially because of this:
                          i upload some php file in that directory and he reads it as a file (var_dump says TRUE!!!)
                          i rename THAT SAME php file from galerije2.php to galerije.php and var_dump says FALSE!!!!!!!!!!!!!!!
                          i upload some jpg or jpeg or JPG or JPEG file and var_dump is FALSE again!!!!

                          GRRRAAAAAAAOOOUUUGGGHHH!!!!! 😕 😕 😕 😕 😕

                            please, anyone?!?! i'm stuck here for days! :queasy: there's really no one who could explain my problem? 🙁

                              Just a hint if you ever see an empty page in PHP you have enbcountered a fatal error and display errors is set to 0. The following code I just ripped out class I use on out live servers so I can see an error of this nature when it comes up.

                              <?php
                              function SetShowErrors( $override = '' )
                              {
                                  $enable_key = "see_errors";
                                  $hide_key   = "hide_errors";
                              
                              if ( isset($_GET[ $hide_key ]) )
                              {
                                  unset($_SESSION[ $enable_key ]);
                                  ini_set( 'display_errors' , 0 );
                              }
                              
                              if ( isset($_SESSION[ $enable_key ]) OR isset($_GET[ $enable_key ]) OR $override )
                              {
                                  $_SESSION[ $enable_key ] = true;
                                  ini_set( 'error_reporting', E_ALL );
                                  ini_set( 'display_errors' , 1 );
                              }
                               }
                              
                               //just run and whenever you append &see_errors to a url visual error reporting turns on. &hide_errors turns it off
                               //eg /index.php?id=1&see_errors
                               SetShowErrors();
                              ?>
                              

                              Unless you have a way to see errors you are flying blind and is a waste of your time. It also is session based so works on form posts. For example on a hosting server it could be in safe mode, disabled functions, register global variables screwing with sessions and all sorts of other stuff which is not default config.

                              Also read this..
                              http://uk2.php.net/manual/en/function.is-file.php#55720

                              It could be permissions problems as your

                              <?
                              if (is_file($file))
                              {
                                  echo ("$file is a regular file");
                              }
                              else
                              {
                                  echo ("$file is not a regular file");
                              }
                              ?>
                              

                              code could actually return false on regular files in a lot of circumstances.

                              Hope this gives a better clue in finding the real problem...

                                When using is_file(), you need to include the path to the file !!

                                If you read the directory "image/galerije/ups/5" (as $strDirectory) and the file "IMG_2900.jpg" (as $strFile)...

                                is_file($strFile) -> false
                                is_file($strDirectory . '/' . $strFile) -> true

                                  thanks suntra, now everything works as it should...
                                  i simply made one more variable which hold the full path to the file...
                                  now, finally to get in touch with other problems 🙂
                                  thank you everybody for help!!

                                    Write a Reply...