<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>opendir &#8211; LPC影子技术分享</title>
	<atom:link href="https://www.mudbest.com/tag/opendir/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.mudbest.com</link>
	<description>行影不离,忘之却步.</description>
	<lastBuildDate>Mon, 08 Oct 2012 09:22:34 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.1.2</generator>
	<item>
		<title>PHP获取目录以及文件</title>
		<link>https://www.mudbest.com/php%e8%8e%b7%e5%8f%96%e7%9b%ae%e5%bd%95%e4%bb%a5%e5%8f%8a%e6%96%87%e4%bb%b6/</link>
		
		<dc:creator><![CDATA[hkshadow]]></dc:creator>
		<pubDate>Mon, 08 Oct 2012 09:21:16 +0000</pubDate>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[closedir]]></category>
		<category><![CDATA[opendir]]></category>
		<category><![CDATA[readdir]]></category>
		<category><![CDATA[目录]]></category>
		<category><![CDATA[递归]]></category>
		<category><![CDATA[遍历]]></category>
		<guid isPermaLink="false">http://www.mudbest.com/?p=1553</guid>

					<description><![CDATA[用到的系统函数有:opendir(),readdir(),closedir()。 opendir()： 打开一 ... <a title="PHP获取目录以及文件" class="read-more" href="https://www.mudbest.com/php%e8%8e%b7%e5%8f%96%e7%9b%ae%e5%bd%95%e4%bb%a5%e5%8f%8a%e6%96%87%e4%bb%b6/" aria-label="阅读 PHP获取目录以及文件">阅读更多</a>]]></description>
										<content:encoded><![CDATA[<p>用到的系统函数有:opendir(),readdir(),closedir()。</p>
<p><strong>opendir()：</strong></p>
<pre class="brush: php; title: ; notranslate">
resource opendir ( string $path &#x5B;, resource $context ] )
//path 要打开的目录路径 
//context context 参数的说明见手册中的 Streams API 一章。 
</pre>
<p>打开一个目录句柄，可用于之后的 closedir()，readdir() 和 rewinddir() 调用中。 <span id="more-1553"></span></p>
<p>Example #1 opendir() 例子</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
$dir = &quot;/etc/php5/&quot;;

// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
            echo &quot;filename: $file : filetype: &quot; . filetype($dir . $file) . &quot;\n&quot;;
        }
        closedir($dh);
    }
}
?&gt; 
</pre>
<p><strong>readdir()：</strong></p>
<pre class="brush: php; title: ; notranslate">
resource opendir ( string $path &#x5B;, resource $context ] )
//dir_handle 目录句柄的 resource，之前由 opendir() 打开 
</pre>
<p>返回目录中下一个文件的文件名。文件名以在文件系统中的排序返回。 </p>
<p>Example #1 列出目录中的所有文件</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
// 注意在 4.0.0-RC2 之前不存在 !== 运算符

if ($handle = opendir('/path/to/files')) {
    echo &quot;Directory handle: $handle\n&quot;;
    echo &quot;Files:\n&quot;;

    /* 这是正确地遍历目录方法 */
    while (false !== ($file = readdir($handle))) {
        echo &quot;$file\n&quot;;
    }

    /* 这是错误地遍历目录的方法 */
    while ($file = readdir($handle)) {
        echo &quot;$file\n&quot;;
    }

    closedir($handle);
}
?&gt; 
</pre>
<p>Example #2 列出当前目录的所有文件并去掉 . 和 ..</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
if ($handle = opendir('.')) {
    while (false !== ($file = readdir($handle))) {
        if ($file != &quot;.&quot; &amp;&amp; $file != &quot;..&quot;) {
            echo &quot;$file\n&quot;;
        }
    }
    closedir($handle);
}
?&gt; 
</pre>
<p><strong>closedir()：</strong></p>
<pre class="brush: php; title: ; notranslate">
void closedir ( resource $dir_handle )
//dir_handle 目录句柄的 resource，之前由 opendir() 所打开的。 
</pre>
<p>关闭由 dir_handle 指定的目录流。流必须之前被 opendir() 所打开。 </p>
<p>Example #1 closedir() 例子</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
$dir = &quot;/etc/php5/&quot;;

// Open a known directory, read directory into variable and then close
if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        $directory = readdir($dh);
        closedir($dh);
    }
}
?&gt; 
</pre>
<p><strong>代码部分：</strong></p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
/**
 * 获取目录以及文件 date : 2012-10-08
 */
function get_file($path){
	$current_dir = opendir($path);  //openfile()返回一个目录句柄，失败则false
	while (($file = readdir($current_dir)) !==false){ //readdir()返回打开目录句柄中的一个条目
		$sub_dir = $path . DS . $file; //形成子目录形式
		if($file == '.' || $file == '..'){ 
			continue;
		}else if(is_dir($sub_dir)){ //如果是有效子目录则递归调用
			echo 'directory '.$file;
			echo &quot;&lt;br&gt;&quot;;
			get_file($path); //调用自身
			closedir($current_dir);//
		}else{
			echo 'file in directory '. $path. ':' .$file;
		}
	}
}
?&gt;
</pre>
<p>实例：</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
get_file('./11');
?&gt;
</pre>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
