ok i m trying to delete folder with all contents ... the contents r all files..its not working...plz help

<? 
$mydir = "d:/lucky"; 
$d = dir($mydir); 
while($entry = $d->readfile()) { 
 if ($entry!= "." && $entry!= "..") { 
 unlink($entry); 
 } 
} 
$d->close(); 
rmdir($mydir); 
?>

Recommended Answers

All 10 Replies

I found a nice little script that will do the job but won't delete 2 billion subdirectories due to a permissions denied error.

function delete_directory($dirname) {
    if (is_dir($dirname))
        $dir_handle = opendir($dirname);
    if (!$dir_handle)
        return false;
    while($file = readdir($dir_handle)) {
        if ($file != "." && $file != "..") {
            if (!is_dir($dirname."/".$file))
                unlink($dirname."/".$file);
            else
                delete_directory($dirname.'/'.$file);          
        }
    }
    closedir($dir_handle);
    rmdir($dirname);
    return true;
}

Below is the web address I found the code:
http://www.ozzu.com/programming-forum/php-delete-directory-folder-t47492.html

ok this is wat i did and its not working i have a folder names lucky in d:/ with a file width.js .... this si the code i tried to delete the folder witht its content but its not working...where m i going wrong

<?
 $dir = "d:/lucky";
 $result = delete_directory($dir) ;
if  ($result==true) {
echo " dir deleted";}
else {echo "dir not deleted ";}
  
 function delete_directory($dirname) {
    if (is_dir($dirname))
        $dir_handle = opendir($dirname);
    if (!$dir_handle)
        return false;
    while($file = readdir($dir_handle)) {
        if ($file != "." && $file != "..") {
            if (!is_dir($dirname."/".$file))
                unlink($dirname."/".$file);
            else
                delete_directory($dirname.'/'.$file);          
        }
    }
    closedir($dir_handle);
    rmdir($dirname);
    return true;
}
 ?>
$dir = "d:/lucky";

I've seen that mistake been made so many times but I shall explain again. The path is incorrect because you need to specify the relative location without the drive letter. So this means that you cannot have your domain in the path nor can you specify the drive letter like you have done previously.
If your web directory is d:/htdocs/ then you will need to specify the following although the following assumes that htdocs is always in the base of drive d

$dir = "/lucky";

However that script on a Linux or Unix server in general not work and certainly will never work on a shared server as the folder is being created in the root of the drive. If you plan to place the directory on your server then you may need to place it within your web directory meaning you will need to change the code for that. However, for the info within the quote, the above code should work if the web directory in the apache service is set to the root of drive d.

yes the apache is in d:/wamp i created a folder using mkdir on d:/ with name lucky ...now i want to delete tht ...i tried moving the folder lucky into the folder having .php script and tested still its not working

btw i changed it to /lucky still its not working

yes the apache is in d:/wamp i created a folder using mkdir on d:/ with name lucky ...now i want to delete tht ...i tried moving the folder lucky into the folder having .php script and tested still its not working

So you made the directory with a php script. At the moment there are only two other options I can think of both using the function I provided earlier. They are a- place in the $dir= the same as you placed in the mkdir function. And b- keep on using ../ in the $dir= until your at the base of the drive then specify lucky
Example: ../../../lucky

Always go in deep if the directory is within other directory like
abc/xyz/001

if go outward

../../abc

actually i implemented both ways they r not working

actually i implemented both ways they r not working

I just tried some of the previously posted examples and the ones I have tested so far all seem to work for me but the only difference is I'm using drive C. Below is several examples that work for me:

<?
function delete_directory($dirname) {
    if (is_dir($dirname))
        $dir_handle = opendir($dirname);
    if (!$dir_handle)
        return false;
    while($file = readdir($dir_handle)) {
        if ($file != "." && $file != "..") {
            if (!is_dir($dirname."/".$file))
                unlink($dirname."/".$file);
            else
                delete_directory($dirname.'/'.$file);          
        }
    }
    closedir($dir_handle);
    rmdir($dirname);
    return true;
}
//now the function is defined the examples can be used
delete_directory('C:/folder');
//or
delete_directory('/folder');
//or
delete_directory('../../folder');
?>

So the only two things I can think of is that your either not placing the functions raw code like in the above and assuming it is part of an internal library or that there is a configuration on your server preventing such action from being taken. You may even need to do a checkdisk to clean up the errors on your harddrive as I had this problem when creating 2 billion folders. To perform a checkdisk, just click start->run then type cmd. A black window will appear and in the window type CHKDSK /F while in the appropriate drive. To learn how to use that command line just read up on msdos.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.