<?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>Gudubeth &#187; file operations</title>
	<atom:link href="http://www.gudubeth.net/en/tag/file-operations/feed" rel="self" type="application/rss+xml" />
	<link>http://www.gudubeth.net/en</link>
	<description>A blog about web design and programming - php, actionscript, javascript, java, css</description>
	<lastBuildDate>Tue, 27 Apr 2010 19:08:26 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Remove a Directory and Its Content with PHP</title>
		<link>http://www.gudubeth.net/en/articles/remove-directory-with-php</link>
		<comments>http://www.gudubeth.net/en/articles/remove-directory-with-php#comments</comments>
		<pubDate>Sat, 09 Jan 2010 00:18:44 +0000</pubDate>
		<dc:creator>Gudubeth</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[file operations]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.gudubeth.net/en/?p=60</guid>
		<description><![CDATA[The function below deletes directories with many options. You can delete directories recursively, delete only content of directories or remove files in a directory with regular expression. I wrote details in PHPDoc format. 

/**
 * removes files in a directory
 * examples:
 * &#60;pre&#62;
 * removeDir('/home/gudubeth/pics');
 *      //delete everything and [...]


Related posts:<ol><li><a href='http://www.gudubeth.net/en/articles/remove-accents-in-utf-8-strings' rel='bookmark' title='Permanent Link: Remove Accents In UTF-8 Strings'>Remove Accents In UTF-8 Strings</a></li></ol>]]></description>
			<content:encoded><![CDATA[<p>The function below deletes directories with many options. You can delete directories recursively, delete only content of directories or remove files in a directory with regular expression. I wrote details in PHPDoc format. </p>
<pre lang="php">
/**
 * removes files in a directory
 * examples:
 * &lt;pre&gt;
 * removeDir('/home/gudubeth/pics');
 *      //delete everything and the directory itself.
 * removeDir('/home/gudubeth/pics', false);
 *      //delete only content of the directory. subdirectories are deleted too.
 * removeDir('/home/gudubeth/pics', false, false);
 *      //delete only content of the directory. subdirectories
 *      //are NOT deleted
 * removeDir('/home/gudubeth/pics', false, true, '/.*\.jpg/');
 *      //delete only jpg files inside this directory and its sub directories.
 * &lt;/pre&gt;
 * @param str $dir  directory path
 * @param bool $deleteDir   deletes the directory itself if it is true.
 *                          default is true
 * @param bool $recursive   deletes files recursively if it is true.
 *                          default is true
 * @param str $regEx        deletes files only if they match this $regEx.
 *                          Regular expression checking is done with preg_match.
 *                          default is '/.*/' which means 'everything'
 * @return  bool    true if every file is deleted successfully or there are
 *                  no files found to delete. false if any of the deletions
 *                  is unsuccessful
 *
 * */
function removeDir($dir, $deleteDir=true, $recursive=true, $regEx="/.*/") {
    if(!$dh = @opendir($dir)) return false;
    $result=true;
    while (($file=readdir($dh))!==false) {
        if($file!='.' &#038;&#038; $file!='..'){
            if(is_dir( $dir.'/'.$file) &#038;&#038; $recursive)
                $result = removeDir($dir.'/'.$file, true, true, $regEx) &#038;&#038; $result;
            else if(preg_match($regEx, $file))
                $result = @unlink($dir.'/'.$file) &#038;&#038; $result;
        }
    }
    closedir($dh);
    if($deleteDir) $result = @rmdir($dir) &#038;&#038; $result;
    return $result;
}
</pre>


<p>Related posts:<ol><li><a href='http://www.gudubeth.net/en/articles/remove-accents-in-utf-8-strings' rel='bookmark' title='Permanent Link: Remove Accents In UTF-8 Strings'>Remove Accents In UTF-8 Strings</a></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.gudubeth.net/en/articles/remove-directory-with-php/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
