<?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>Side Notes &#187; Code</title>
	<atom:link href="http://blog.snaky.org/tag/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.snaky.org</link>
	<description>Relax, its only ONES and ZEROS!</description>
	<lastBuildDate>Sun, 11 Oct 2009 12:20:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
<image>
<link>http://blog.snaky.org</link>
<url>http://blog.snaky.org/wp-content/plugins/maxblogpress-favicon/icons/favicon-26.ico</url>
<title>Side Notes</title>
</image>
		<item>
		<title>Verzeichnisse rekursiv/iterativ durchsuchen</title>
		<link>http://blog.snaky.org/2008/07/03/php-2-verzeichnisse-rekursiviterativ-durchsuchen/</link>
		<comments>http://blog.snaky.org/2008/07/03/php-2-verzeichnisse-rekursiviterativ-durchsuchen/#comments</comments>
		<pubDate>Thu, 03 Jul 2008 21:02:14 +0000</pubDate>
		<dc:creator>Seb</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Anleitung]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Guide]]></category>
		<category><![CDATA[Index]]></category>
		<category><![CDATA[Seitenindex]]></category>

		<guid isPermaLink="false">http://blog.snaky.org/?p=20</guid>
		<description><![CDATA[<img src="http://blog.snaky.org/wp-content/uploads/2008/07/php_16.png" width="16" height="16" alt="" title="PHP" /><br/>Tja, da gibt man sich solche Mühe. Hat sich sogar schon einen Algorithmus zusammengebastelt und sucht nur noch die ganzen PHP-Methoden bzw. überprüft ob PHP das auch kann. Dann stöbert man ein bisschen in den Kommentaren zu den Funktionen.. ..und was entdeckt man? Einen drei Zeiler, der die Verzeichnisse komplett durchforstet und alle Unterverzeichnisse auflistet. [...]]]></description>
			<content:encoded><![CDATA[<img src="http://blog.snaky.org/wp-content/uploads/2008/07/php_16.png" width="16" height="16" alt="" title="PHP" /><br/><p>Tja, da gibt man sich solche Mühe. Hat sich sogar schon einen Algorithmus zusammengebastelt und sucht nur noch die ganzen PHP-Methoden bzw. überprüft ob PHP das auch kann. Dann stöbert man ein bisschen in den Kommentaren zu den Funktionen..</p>
<p>..und was entdeckt man?<br />
Einen <strong>drei</strong> Zeiler, der die Verzeichnisse komplett durchforstet und alle Unterverzeichnisse auflistet.<br />
Klassischer Fall:<br />
<span id="more-20"></span></p>
<p><a href="http://blog.snaky.org/wp-content/uploads/2008/07/dogownd.jpg"><img class="alignnone size-full wp-image-21" title="dogownd" src="http://blog.snaky.org/wp-content/uploads/2008/07/dogownd.jpg" alt="" width="405" height="540" /></a></p>
<p>Der Vollständigkeit halber hier der Code-Schnipsel:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$it</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> RecursiveDirectoryIterator<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'./'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// RecursiveIteratorIterator accepts the following modes:</span>
<span style="color: #666666; font-style: italic;">//     LEAVES_ONLY = 0  (default)</span>
<span style="color: #666666; font-style: italic;">//     SELF_FIRST  = 1</span>
<span style="color: #666666; font-style: italic;">//     CHILD_FIRST = 2</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> RecursiveIteratorIterator<span style="color: #009900;">&#40;</span><span style="color: #000088;">$it</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$path</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$path</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isDir</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$path</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Also hingenangen und ausprobiert.<br />
Doch zu früh gefreut. Es gibt zwar alle Pfade aus, aber leider nur die tiefstens Pfade. Gibt es z.B. das Verzeichnis /partent mit den Unterverzeichnissen /child_1 und /child_2 gibt die Methode /partent/child_1 und /partent/child_2 aus. Da aber auch im /parten-Verzeichnis Daten sein können, funktioniert das schonmal nicht so, wie wir uns das vorstellen.<br />
(Außerdem werden die Verzeichnisse mit \ ausgeben. Nicht sehr hilfreich.)</p>
<p>Aber nicht nur Romeo&amp;Julia hat ein Happy End. Auch meine Geschichte hat ein gutes Ende. Denn beim weiteren Durchstöbern der PHP-Funktionen fand ich diesen Code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
     <span style="color: #000000; font-weight: bold;">function</span> get_leaf_dirs<span style="color: #009900;">&#40;</span><span style="color: #000088;">$dir</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
         <span style="color: #000088;">$array</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
         <span style="color: #000088;">$d</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #990000;">dir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dir</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
         <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$d</span><span style="color: #009900;">&#41;</span>
         <span style="color: #009900;">&#123;</span>
               <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">false</span> <span style="color: #339933;">!==</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$entry</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$d</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">read</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
               <span style="color: #009900;">&#123;</span>
                   <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$entry</span><span style="color: #339933;">!=</span><span style="color: #0000ff;">'.'</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$entry</span><span style="color: #339933;">!=</span><span style="color: #0000ff;">'..'</span><span style="color: #009900;">&#41;</span>
                   <span style="color: #009900;">&#123;</span>
                       <span style="color: #000088;">$entry</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;">$entry</span><span style="color: #339933;">;</span>
                       <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">is_dir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$entry</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
                       <span style="color: #009900;">&#123;</span>
                           <span style="color: #000088;">$subdirs</span> <span style="color: #339933;">=</span> get_leaf_dirs<span style="color: #009900;">&#40;</span><span style="color: #000088;">$entry</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                           <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$subdirs</span><span style="color: #009900;">&#41;</span>
                             <span style="color: #000088;">$array</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_merge</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #339933;">,</span> <span style="color: #000088;">$subdirs</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                           <span style="color: #b1b100;">else</span>
                             <span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$entry</span><span style="color: #339933;">;</span>
                       <span style="color: #009900;">&#125;</span>
                   <span style="color: #009900;">&#125;</span>
               <span style="color: #009900;">&#125;</span>
               <span style="color: #000088;">$d</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
         <span style="color: #009900;">&#125;</span>
         <span style="color: #b1b100;">return</span> <span style="color: #000088;">$array</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
&nbsp;
	  <span style="color: #000088;">$dir</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'./'</span><span style="color: #339933;">;</span>
	  <span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span>get_leaf_dirs<span style="color: #009900;">&#40;</span><span style="color: #000088;">$dir</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>..und was soll ich sagen. Ich bin so glücklich. Ich könnte weinen. Aber ich tus nich! Bin ja schließlich nen Mann.</p>
<p>Der nächste Schritt ist also den <a href="http://blog.snaky.org/2008/07/03/php-1-seitenindex-erstellen/">alten Code</a> anpassen.</p>
<p>So long,<br />
Seb</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.snaky.org/2008/07/03/php-2-verzeichnisse-rekursiviterativ-durchsuchen/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
