Jump to content


This topic is now archived and is closed to further replies.


OOP Not working quite right

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;
    $d = opendir($directory) or die("Couldn't open directory.");
    while(false !== ($f=readdir($d))){
      $this->filearray[] = $f;
    die("Must pass in a directory.");

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)){

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

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.

Share this post

Link to post
Share on other sites


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.