<?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>piouPiouM&#039;s dev&#187; Archives pour le tag RecursiveDirectoryIterator – piouPiouM&#039;s dev</title>
	<atom:link href="http://pioupioum.fr/tag/recursivedirectoryiterator/feed/" rel="self" type="application/rss+xml" />
	<link>http://pioupioum.fr</link>
	<description>Bloc-note d&#039;un développeur web</description>
	<lastBuildDate>Fri, 23 Dec 2011 23:41:36 +0000</lastBuildDate>
	
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>PHP&#160;: supprimer récursivement un répertoire</title>
		<link>http://pioupioum.fr/developpement/php-spl-supprimer-repertoire-recursif.html</link>
		<comments>http://pioupioum.fr/developpement/php-spl-supprimer-repertoire-recursif.html#comments</comments>
		<pubDate>Fri, 17 Jul 2009 17:47:02 +0000</pubDate>
		<dc:creator>piouPiouM</dc:creator>
				<category><![CDATA[Développement Web]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[RecursiveDirectoryIterator]]></category>
		<category><![CDATA[RecursiveIteratorIterator]]></category>
		<category><![CDATA[SPL]]></category>

		<guid isPermaLink="false">http://pioupioum.fr/?p=95</guid>
		<description><![CDATA[Voici une petite fonction pour supprimer un répertoire et toute son arborescence. J&#8217;utilise ici la Standard PHP Library (SPL) et notamment son itérateur RecursiveDirectoryIterator qui permet de traverser récursivement un répertoire.

Il est important de noter l&#8217;utilisation de la constante RecursiveIteratorIterator::CHILD_FIRST. Grâce à elle, l&#8217;itérateur, instance de la classe RecursiveIteratorIterator, va parcourir récursivevement les fichiers des [...]]]></description>
			<content:encoded><![CDATA[<!-- google_ad_section_start --><p>Voici une petite fonction pour supprimer un répertoire et toute son arborescence. J&#8217;utilise ici la <a href="http://fr.php.net/spl" title="PHP: SPL - Manual">Standard PHP Library</a> (SPL) et notamment son itérateur <strong>RecursiveDirectoryIterator</strong> qui permet de traverser récursivement un répertoire.</p>

<p>Il est important de noter l&#8217;utilisation de la constante <code>RecursiveIteratorIterator::CHILD_FIRST</code>. Grâce à elle, l&#8217;itérateur, instance de la classe <strong>RecursiveIteratorIterator</strong>, va parcourir récursivevement les fichiers des dossiers. Après avoir supprimés les fichiers, l&#8217;itérateur passe au dossier parent qui pourra être à son tour détruit. En effet, la fonction <a href="http://fr.php.net/manual/fr/function.rmdir.php" title="PHP: rmdir - Manual"><code>rmdir</code></a> ne peut qu&#8217;effacer un dossier vide.
<span id="more-95"></span></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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
</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: #009933; font-style: italic;">/**
 * Attempts to remove recursively the directory named by dirname.
 *
 * @author Mehdi Kabab &lt;http://pioupioum.fr&gt;
 * @copyright Copyright (C) 2009 Mehdi Kabab
 * @license http://www.gnu.org/licenses/gpl.html  GNU GPL version 3 or later
 *
 * @param string $dirname Path to the directory.
 * @param boolean $followLinks Removes symbolic links if set to TRUE.
 * @return boolean Returns TRUE on success or FALSE on failure.
 * @throws Exception
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> recursive_rmdir<span style="color: #009900;">&#40;</span><span style="color: #000088;">$dirname</span><span style="color: #339933;">,</span> <span style="color: #000088;">$followLinks</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</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: #990000;">is_dir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dirname</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span><span style="color: #990000;">is_link</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dirname</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: #339933;">!</span><span style="color: #990000;">is_writable</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dirname</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
            throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'You do not have renaming permissions!'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$iterator</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> RecursiveIteratorIterator<span style="color: #009900;">&#40;</span>
            <span style="color: #000000; font-weight: bold;">new</span> RecursiveDirectoryIterator<span style="color: #009900;">&#40;</span><span style="color: #000088;">$dirname</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
            RecursiveIteratorIterator<span style="color: #339933;">::</span><span style="color: #004000;">CHILD_FIRST</span>
        <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$iterator</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">valid</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;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$iterator</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isDot</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;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$iterator</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isWritable</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>
                    throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span>
                        <span style="color: #0000ff;">'Permission Denied: %s.'</span><span style="color: #339933;">,</span>
                        <span style="color: #000088;">$iterator</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPathName</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: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
                <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$iterator</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isLink</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900; font-weight: bold;">false</span> <span style="color: #339933;">===</span> <span style="color: #009900;">&#40;</span>boolean<span style="color: #009900;">&#41;</span> <span style="color: #000088;">$followLinks</span><span style="color: #009900;">&#41;</span>
                <span style="color: #009900;">&#123;</span>
                    <span style="color: #000088;">$iterator</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">next</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;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$iterator</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isFile</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: #990000;">unlink</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$iterator</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPathName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
                <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$iterator</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: #990000;">rmdir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$iterator</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPathName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            <span style="color: #000088;">$iterator</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">next</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: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$iterator</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Fix for Windows.</span>
&nbsp;
        <span style="color: #b1b100;">return</span> <span style="color: #990000;">rmdir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dirname</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">else</span>
    <span style="color: #009900;">&#123;</span>
        throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Directory %s does not exist!'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$dirname</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</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><strong>Mis à jour du 02/08/2009</strong><br />
Suppression de la levée d&#8217;exception sur la suppression du lien symbolique. Le script passe au fichier suivant. Merci à <a href="#comment-21" title="Voir le commentaire">Oncle Tom</a>.</p>

<p><strong>Mis à jour du 03/08/2009</strong><br />
Patch pour Windows. Libération de l&#8217;itérateur qui ferme correctement le répertoire à supprimer. Merci à <a href="#comment-30" title="Voir le commentaire">Yho</a>.</p>

<h3 class='related_post_title'>Continuez votre lecture sur des sujets similaires</h3>

<ul class='related_post'><li><a href='http://pioupioum.fr/developpement/photon-php-installer-mac-os-x-snow-leopard.html' title='Photon : guide d&#8217;installation sur Mac OS X Snow Leopard'>Photon&#160;: guide d&#8217;installation sur Mac OS X Snow Leopard</a></li><li><a href='http://pioupioum.fr/snippets/wordpress-create-shortcode-class.html' title='WordPress : une classe abstraite pour aider la création de shortcodes'>WordPress&#160;: une classe abstraite pour aider la création de shortcodes</a></li><li><a href='http://pioupioum.fr/snippets/textmate-commande-exporter-mots-word-wrap.html' title='TextMate : commande &laquo;&nbsp;Export words&nbsp;&raquo;'>TextMate&#160;: commande &laquo;&nbsp;Export words&nbsp;&raquo;</a></li><li><a href='http://pioupioum.fr/snippets/php-installer-apc-macosx.html' title='PHP : installer APC sous Mac OS X Leopard'>PHP&#160;: installer APC sous Mac OS X Leopard</a></li><li><a href='http://pioupioum.fr/snippets/php-convertir-datetime-unix-timestamp.html' title='PHP : convertir un DATETIME en un timestamp UNIX'>PHP&#160;: convertir un DATETIME en un timestamp UNIX</a></li></ul>
<!-- google_ad_section_end -->]]></content:encoded>
			<wfw:commentRss>http://pioupioum.fr/developpement/php-spl-supprimer-repertoire-recursif.html/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
	</channel>
</rss>

