Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PHP中检查多种图像类型_Php_Image - Fatal编程技术网

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

如何在PHP中检查多种图像类型,php,image,Php,Image,我想要多种图像类型通过我的检查。但是我不知道为什么我的新代码不起作用。谁能帮帮我吗 旧代码(仅适用于jpg) 我想让它通过我想要的所有类型。 我如何让它检查所有这些: $formati=数组(“jpg”、“png”、“gif”、“bmp”) 新代码(不起作用) 您不需要额外的循环。首先,使用获取正在使用的文件的扩展名: $file_ext = pathinfo($file, PATHINFO_EXTENSION); 然后,使用以下命令动态创建正则表达式: 总而言之: $dir =

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

旧代码(仅适用于jpg)


我想让它通过我想要的所有类型。 我如何让它检查所有这些:

$formati=数组(“jpg”、“png”、“gif”、“bmp”)

新代码(不起作用)


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

$file_ext = pathinfo($file, PATHINFO_EXTENSION);
然后,使用以下命令动态创建正则表达式:

总而言之:

$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';
$dir=“img/”;
$ispis=“”;
$formati=数组(“jpg”、“png”、“gif”、“bmp”);
if(is_dir($dir)){
如果($dh=opendir($dir)){
while(($file=readdir($dh))!==false){
$file\u ext=pathinfo($file,pathinfo\u扩展名);
$regex='/'.内爆('|',$formati)。'/';
if(preg_match($regex,$file_ext)){
$putanja=$dir.$file;
$ispis=”;
}
}
closedir($dh);
}
}
包括“_header.php”;

您需要使用或的正则表达式版本-

$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);
    }
}
$dir=“img/”;
$ispis=“”;
$formati=“/.jpg |.png |.gif |.bmp/”);/不是数组
$brojformata=sizeof($valid_格式);
if(is_dir($dir)){
如果($dh=opendir($dir)){
while(($file=readdir($dh))!==false){
if(preg_match($formati,$file)){//不是数组
$putanja=$dir.$file;
$ispis.=“
  • ”; } } closedir($dh); } }
    就个人而言,我认为您应该熟悉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
    }
    

    你有错误吗?您是否进行过调试,以确定脚本执行与预期不同的地方?请查看。
    $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';
    
    $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);
        }
    }
    
    $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
    }