【问题标题】:Read the content of directory in php在php中读取目录的内容
【发布时间】:2018-03-07 22:48:36
【问题描述】:

请我是 php 的初学者,我想读取文件夹中的图像 我的图片名称如下:

image_1_0_0_0.png
image_1_1_0_0.png
image_1_1_1_0.png
.........
.....
image_2_0_0_0.png
image_2_1_0_0.png

image_3_0_0_0.png

我想为以 image_1 _..... 开头的图像分配一种颜色 (imagefilter),为 image_2 _... 分配另一种颜色,为 images_3 .... 分配另一种颜色。

然后我想只读取最后一张图片并只检索数字 3,剩下的第一个数字

我想知道该怎么做。

【问题讨论】:

  • 请问你自己试过什么?
  • 是的,我可以浏览文件夹并为所有图像分配颜色,但我无法将 color1 分配给以 image_1 开头的图像,将 color2 分配给以 image_2 开头的图像 ....
  • 纯代码编写请求在 Stack Overflow 上是题外话——我们希望这里的问题与特定编程问题有关——但我们很乐意帮助您自己编写!告诉我们what you've tried,以及您遇到的问题。这也将有助于我们更好地回答您的问题。
  • 是的,我可以浏览文件夹并为所有图像分配颜色,但我不能将 color1 分配给以 image_1 开头的图像,将 color2 分配给以 image_2 开头的图像 ...

标签: php image file colors imagefilter


【解决方案1】:

读取目录的内容:

$dir = "/images/";
$color = "#000000";

// Open a directory, and read its contents
if (is_dir($dir)){
  if ($dh = opendir($dir)){
    while (($file = readdir($dh)) !== false){
      echo "Filename: ".$file."<br>";
      if($file[7]=='1') $color="#FF0000";
      elseif ($file[7]=='2') $color="#00FF00";
      elseif ($file[7]=='3') $color="#0000FF";
      else $color="#000000";
      // do something with the image
    }
    closedir($dh);
  }
}

我不明白你的最后一个问题。

【讨论】:

  • 我的最后一个问题是:在我的文件夹中,我的图像以 image_1_0_0 ..... 到 image_3_0_0 .... 开头,我只想检索第一个数字 $file [7]如果我在 image_1_0_0 之间有图像......直到 image_4._0_0 我只想检索 4
  • 在您的循环中,您可以执行类似 $myMax = max($myMax, $file[7]) 的操作。但这仅适用于 1 位数字(0 到 9)
猜你喜欢
  • 2019-09-23
  • 2013-11-27
  • 1970-01-01
  • 1970-01-01
  • 2010-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-19
相关资源
最近更新 更多