Jump to content

OOP Not working quite right


interfacer

Recommended Posts

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:
[code]  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.");
    }
  }[/code]

Here's the imageOnly() method:
[code]  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]);
    }
    }
  }[/code]

and finally, here's what I'm trying to output it with:
[code]<?
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 />";
?>[/code]

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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.