1

I want multiple image types to pass my check. But I dont know why my new code doesnt work. Could anyone help me.

Old code (works but only with jpg)

<?php
$dir = "img/";
$ispis = "";

if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
            if (preg_match("/.jpg/", $file)) {
                $putanja = $dir . $file;
                $ispis .= "<li><a href='" . $putanja . "'><img width='100px' height='100px' src='" . $putanja . "'></a></li>";
            }
        }
        closedir($dh);
    }
}

include '_header.php';
?>

I want it to pass all the types I want. How can I make it check all of these:

$formati = array("jpg", "png", "gif", "bmp");

New code (doesnt work)

<?php
$dir = "img/";
$ispis = "";
$formati = array("/.jpg/", "/.png/", "/.gif/", "/.bmp/");
$brojformata = sizeof($valid_formats);
if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
            for( $i = 0; $i < $brojformata; $i++) {
                if (preg_match($formati[$i], $file)) {
                    $putanja = $dir . $file;
                    $ispis .= "<li><a href='" . $putanja . "'><img width='100px' height='100px' src='" . $putanja . "'></a></li>";
                }
            }
        }
        closedir($dh);
    }
}

include '_header.php';
?>
InsaneCricket
  • 125
  • 2
  • 13
  • Do you get any errors? Have you debugged at all to determined where script execution varies from what you expect? – Mike Brant Jun 16 '14 at 17:26
  • Take a look to http://stackoverflow.com/questions/10456113/php-check-file-extension-in-upload-form. – Mihai8 Jun 16 '14 at 17:27

3 Answers3

2

You don't need an additional loop. First, use pathinfo() to get the extension of the file you're working with:

$file_ext = pathinfo($file, PATHINFO_EXTENSION);

Then, create the regular expression dynamically, using implode():

$formati = array("jpg", "png", "gif", "bmp");
$regex   = '/'.implode('|', $formati).'/';

if (preg_match($regex, $file)) {
    // code ...
}

Putting it all together:

$dir     = "img/";
$ispis   = "";
$formati = array("jpg", "png", "gif", "bmp");

if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {

            $file_ext = pathinfo($file, PATHINFO_EXTENSION);
            $regex = '/'.implode('|', $formati).'/';

            if (preg_match($regex, $file_ext)) {
                $putanja = $dir . $file;
                $ispis .= "<html goes here>";
            }
        }
        closedir($dh);
    }
}

include '_header.php';
Amal Murali
  • 75,622
  • 18
  • 128
  • 150
0

You need to use the regex version of OR -

$dir = "img/";
$ispis = "";
$formati = "/.jpg|.png|.gif|.bmp/"); // not an array
$brojformata = sizeof($valid_formats);
if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
            if (preg_match($formati, $file)) { // not an array
                $putanja = $dir . $file;
                $ispis .= "<li><a href='" . $putanja . "'><img width='100px' height='100px' src='" . $putanja . "'></a></li>";
            }
        }
        closedir($dh);
    }
}
Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
0

Personally, I think you should familiarize yourself with DirectoryIterator and SplFileInfo classes.

$path = '/path/to/dir';
$image_ext = array("jpg", "png", "gif", "bmp");

try {
    $dir_iterator = new DirectoryIterator($path);
    foreach($dir_iterator as $file_info) {
        $ext = $file_info->getExtension();
        if(in_array($ext, $image_ext)) {
            // display your HTML
        }
    }
} catch (Exception $e) {
   // do something with Exception
}
Mike Brant
  • 70,514
  • 10
  • 99
  • 103