写了一个函数,与大家分享,是用的递归方法,PHP操作目录下及其子目录下文件除了用递归,我也没找到好的方法,如果大家有好的方法,也可以与我分享。

PHP代码如下,可能不太完美,没怎么去完善,用的话,可以按需要进行修改。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<span style="color: #000000;font-weight: bold"><?php</span>
<span style="color: #666666;font-style: italic">/*
 * PHP 读取目录下及其子目下的文件函数
 * */</span>
<span style="color: #000088">$files</span><span style="color: #339933">=</span>getFile<span style="color: #009900">(</span><span style="color: #0000ff">"images"</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//调用函数</span>
<span style="color: #b1b100">foreach</span> <span style="color: #009900">(</span><span style="color: #000088">$files</span> <span style="color: #b1b100">as</span> <span style="color: #000088">$name</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
	<span style="color: #b1b100">echo</span> <span style="color: #0000ff">"<span style="color: #006699;font-weight: bold">$name</span>"</span><span style="color: #339933">.</span><span style="color: #0000ff">"<br>"</span><span style="color: #339933">;</span>
<span style="color: #009900">}</span>
 
<span style="color: #000000;font-weight: bold">function</span> getFile<span style="color: #009900">(</span><span style="color: #000088">$dir</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
	<span style="color: #000088">$files</span><span style="color: #339933">=</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/array" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">array</span></a><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
	<span style="color: #b1b100">if</span> <span style="color: #009900">(</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/is_file" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">is_file</span></a><span style="color: #009900">(</span><span style="color: #000088">$dir</span><span style="color: #009900">)</span><span style="color: #009900">)</span><span style="color: #009900">{</span><span style="color: #666666;font-style: italic">//如果指定路径为文件直接返回</span>
		<span style="color: #b1b100">return</span> <span style="color: #000088">$dir</span><span style="color: #339933">;</span>
	<span style="color: #009900">}</span>
 
	<span style="color: #000088">$hande</span><span style="color: #339933">=</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/opendir" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">opendir</span></a><span style="color: #009900">(</span><span style="color: #000088">$dir</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
 
	<span style="color: #b1b100">if</span> <span style="color: #009900">(</span><span style="color: #000088">$hande</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
		<span style="color: #b1b100">while</span> <span style="color: #009900">(</span><span style="color: #009900;font-weight: bold">false</span><span style="color: #339933">!==</span><span style="color: #009900">(</span><span style="color: #000088">$ufile</span><span style="color: #339933">=</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/readdir" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">readdir</span></a><span style="color: #009900">(</span><span style="color: #000088">$hande</span><span style="color: #009900">)</span><span style="color: #009900">)</span><span style="color: #009900">)</span><span style="color: #009900">{</span><span style="color: #666666;font-style: italic">//如果读取目录成功</span>
			<span style="color: #b1b100">if</span> <span style="color: #009900">(</span><span style="color: #000088">$ufile</span><span style="color: #339933">!=</span><span style="color: #0000ff">"."</span> <span style="color: #339933">&&</span> <span style="color: #000088">$ufile</span><span style="color: #339933">!=</span><span style="color: #0000ff">".."</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
				<span style="color: #000088">$filename</span><span style="color: #339933">=</span><span style="color: #000088">$dir</span><span style="color: #339933">.</span><span style="color: #0000ff">"/"</span><span style="color: #339933">.</span><span style="color: #000088">$ufile</span><span style="color: #339933">;</span>
				<span style="color: #b1b100">if</span> <span style="color: #009900">(</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/is_file" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">is_file</span></a><span style="color: #009900">(</span><span style="color: #000088">$filename</span><span style="color: #009900">)</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
					<span style="color: #000088">$files</span><span style="color: #009900">[</span><span style="color: #009900">]</span><span style="color: #339933">=</span><span style="color: #000088">$filename</span><span style="color: #339933">;</span>
				<span style="color: #009900">}</span><span style="color: #b1b100">else</span><span style="color: #009900">{</span>
					<span style="color: #000088">$files</span><span style="color: #339933">=</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/array_merge" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">array_merge</span></a><span style="color: #009900">(</span><span style="color: #000088">$files</span><span style="color: #339933">,</span>getFile<span style="color: #009900">(</span><span style="color: #000088">$filename</span><span style="color: #009900">)</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
				<span style="color: #009900">}</span>
			<span style="color: #009900">}</span>
		<span style="color: #009900">}</span><span style="color: #666666;font-style: italic">//end While</span>
		<a rel="noopener noreferrer nofollow" href="http://www.php.net/closedir" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">closedir</span></a><span style="color: #009900">(</span><span style="color: #000088">$hande</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
	<span style="color: #009900">}</span><span style="color: #666666;font-style: italic">//end if</span>
 
	<span style="color: #b1b100">return</span> <span style="color: #000088">$files</span><span style="color: #339933">;</span>
<span style="color: #009900">}</span>
<span style="color: #000000;font-weight: bold">?></span>