472,096 Members | 1,289 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,096 software developers and data experts.

recursive delete all files/folders

Dear guru

I want to delete all file and folder recursivly under php code, can
anyone give me commend for this.

Thank very much

Sep 13 '05 #1
5 19953
be*******@gmail.com wrote:
Dear guru

I want to delete all file and folder recursivly under php code, can
anyone give me commend for this.

Thank very much


Hi,

It is not too complicated.
But pay attention to potential problems concerning file and directory
permissions.
Remember that PHP runs as user 'nobody' or 'apache' or 'www-data' on most
*nix machines, and as IUSR_XXX on W$ machines/IIS.

That user needs appropriate right to delete files and directories.

For some reference and code-examples, start reading here:
http://nl2.php.net/dir
and
http://nl2.php.net/manual/en/ref.filesystem.php

You will probably use functions like rmdir() and unlink().

Good luck!

Regards,
Erwin Moller

Sep 13 '05 #2
Something like the following should work. Untested though.

function delete_dir($path) {
$files = glob("$path/*");
foreach($files as $file) {
if(is_dir($file) && !is_link($file)) {
delete_dir($file);
}
else {
unlink($p);
}
}
rmdir($path);
}

Sep 13 '05 #3

deletes a file, or a folder and everything in it, whatever you put into
$dirname.

function rmdirr($dirname)
{
// Sanity check
if (!file_exists($dirname)) {
echo "no directory by that name";
return false;
}

// Simple delete for a file
if (is_file($dirname)) {
return unlink($dirname);
}

// Loop through the folder
$dir = dir($dirname);
while (false !== $entry = $dir->read()) {
// Skip pointers
if ($entry == '.' || $entry == '..') {
continue;
}

// Recurse
rmdirr("$dirname/$entry");
}// end while looping

// Clean up
$dir->close();
return rmdir($dirname);

}

Sep 13 '05 #4
be*******@gmail.com wrote:
Dear guru

I want to delete all file and folder recursivly under php code, can
anyone give me commend for this.

Thank very much

I wrote a function to do that a few weeks ago that was based on a
recursive copy function (for an application script on my local Windows
machine), but when it came time to test it, I chickened out and just
wrote a script to remove the specific folders used in my application.

Be careful. Those files do not go to the recycle bin.

--
*****************************
Chuck Anderson • Boulder, CO
http://www.CycleTourist.com
Integrity is obvious.
The lack of it is common.
*****************************
Sep 14 '05 #5
be*******@gmail.com wrote:
Dear guru

I want to delete all file and folder recursivly under php code, can
anyone give me commend for this.


/// Clean directory
/** Delete all files in directory
* @param $path directory to clean
* @param $recursive delete files in subdirs
* @param $delDirs delete subdirs
* @param $delRoot delete root directory
* @access public
* @return success
*/
function cleanDir($path,$recursive=true,$delDirs=false,$del Root=null)
{
$result=true;
if($delRoot===null) $delRoot=$delDirs;
if(!$dir=@dir($path)) return false;
while($file=$dir->read())
{
if($file==='.' || $file==='..') continue;

$full=$dir->path.DIRECTORY_SEPARATOR.$file;
if(is_dir($full) && $recursive)
{
$result&=filesys::cleanDir($full,$recursive,$delDi rs,$delDirs);
}else if(is_file($full))
{
$result&=unlink($full);
}
}
$dir->close();
if($delRoot)
{
$result&=rmdir($dir->path);
}
return $result;
}
Oct 7 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by =?Utf-8?B?RGF2aWQgQ29sbGl2ZXI=?= | last post: by
4 posts views Thread by - HAL9000 | last post: by
3 posts views Thread by Matt | last post: by
9 posts views Thread by Lloyd Sheen | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.