Jump to content


Photo

OOP Not working quite right


  • Please log in to reply
No replies to this topic

#1 interfacer

interfacer
  • Members
  • Pip
  • Newbie
  • 1 posts

Posted 07 October 2006 - 11:45 PM

Hi All :)  I recently just purchased a book on OOP in PHP 5 and have been working through it.  I ran into a wall when I've been trying to use an imageOnly() method to sort out any non image files before they are displayed. 

Here is the constructor:
   public function __construct($directory, $replacechar = "_") {
    $this->directory = $directory;
    $this->replacechar=$replacechar;
    $d="";
    if(is_dir($directory)){
     $d = opendir($directory) or die("Couldn't open directory.");
     while(false !== ($f=readdir($d))){
      if(is_file("$directory/$f")){
       $this->filearray[] = $f;
      }
     }
     closedir($d);
    }else{
     //error
     die("Must pass in a directory.");
    }
   }

Here's the imageOnly() method:
   public function imagesOnly(){
    $extension = "";
    $types = array("jpg", "jpeg", "gif", "png");
    foreach($this->filearray as $key => $value){
     $extension = substr($key,(strpos($key, ".") + 1));
     $extension = strtolower($extension);
     if(!in_array($extension, $types)){
      unset($this->filearray[$key]);
     }
    }
   }

and finally, here's what I'm trying to output it with:
<?
require 'DirectoryItems.php';
$directory = "test";
$di = new DirectoryItems($directory);
$di->naturalCaseInsensitiveOrder();
$di->imagesOnly() or die("imagesOnly() isn't working");
$filearray=$di->getFileArray();

echo "<div style=\"text-align: center;\">";
foreach($filearray as $key => $value) {
 echo "<img src=\"test/$value\"><br />\n";
}
echo "</div><br />";
?>

My class file is named as DirectoryItems.php. if I take out the imagesOnly line, it works fine as long as there are no non-image files in my directory.  Any help in assessing why that method is causing it to appear as "imagesOnly() isn't working" would be wonderful.  Thank you all for your help, I sincerely appreciate it.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users