【问题标题】:How to check multiple image types in PHP如何在 PHP 中检查多种图像类型
【发布时间】:2014-08-06 14:09:25
【问题描述】:

我希望多种图像类型通过我的检查。但我不知道为什么我的新代码不起作用。谁能帮帮我。

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

【问题讨论】:

标签: php image


【解决方案1】:

您不需要额外的循环。首先,使用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';

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      就个人而言,我认为您应该熟悉 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
      }
      

      【讨论】:

        猜你喜欢
        • 2015-07-18
        • 1970-01-01
        • 1970-01-01
        • 2021-11-24
        • 1970-01-01
        • 2011-03-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多