search
Homephp教程php手册php获取目录所有文件并将结果保存到数组的程序

工作中有时会遇到需要将某个目录下的文件进行批量处理的操作,这个时候我们就需要将该目录下的所有符合条件的文件找出来并保存到一个结果集中,然后方便批量处理,通常做法是将其保存到一个数组中,然后循环处理,下面将该过程做一下记录。

<?php
//php获取目录所有文件并将结果保存到数组
foreach(glob("./*") as $d){
	$tmp=explode(".",$d);
	$k=end($tmp);
	//如果是文件,并且后缀名为jpg png的文件
	if(is_file($d)&&in_array($k,array("jpg","png"))){
		$files[]=$d;
	}
}
echo "<pre class="brush:php;toolbar:false">";print_r($files);

以上源代码是列出当前目录下所有后缀为jpg png的文件,将结果保存为数组然后打印出来,本次试验结果打印如下:

您可能感兴趣的文章

  • PHP 生成连续的数字(字母)数组函数range()分析,PHP抽奖程序函数
  • php读取目录并列表显示目录中的文件的函数
  • php如何清除html格式并去除文字中的空格然后截取文字
  • PHP分析文件头信息判断上传文件的类型
  • php获取某段时间内每个月的方法,返回由这些月份组成的数组
  • PHP删除目录及目录下所有文件
  • PHP实现即时将结果输出到浏览器
  • php获取远程图片并下载保存到本地



Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools