如何在PHP中检查多个图像类型

时间:2014-06-16 17:23:05

标签: php image

我希望多种图片类型通过我的检查。但我不知道为什么我的新代码不起作用。任何人都可以帮助我。

旧代码(仅适用于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';
?>

我想让它传递我想要的所有类型。 我怎样才能检查所有这些:

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

新代码(不起作用)

<?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';
?>

3 个答案:

答案 0 :(得分:2)

您不需要额外的循环。首先,使用pathinfo()获取您正在使用的文件的扩展名:

$file_ext = pathinfo($file, PATHINFO_EXTENSION);

然后,使用implode()动态创建正则表达式:

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

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

全部放在一起:

$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';

答案 1 :(得分:0)

您需要使用正则表达式的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);
    }
}

答案 2 :(得分:0)

我个人认为你应该熟悉DirectoryIterator和SplFileInfo类。

$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
}