Log in

View Full Version : Resolved (solved) delete dir and content



dsjoes
03-16-2011, 01:30 PM
how do i get this to delete subfolders and its files aswell

$mydir = $path.$file;
$d = dir($mydir);
while($entry = $d->read()) {
if ($entry!= "." && $entry!= "..") {
unlink($mydir."/".$entry);

}
}
$d->close();
rmdir($mydir);

Beverleyh
03-16-2011, 01:42 PM
Try some recursive delete functions;

http://lixlpixel.org/recursive_function/php/recursive_directory_delete/
http://www.php.happycodings.com/File_Manipulation/code44.html
http://www.dreamincode.net/code/snippet1225.htm

bluewalrus
03-16-2011, 02:02 PM
DJR33 wrote one at the end of this thread. http://www.dynamicdrive.com/forums/showthread.php?t=61310

dsjoes
03-16-2011, 02:03 PM
i have tryed the below but it just refreshes the page nothing happens

<?php
$path = "../../gallery2.0/gallery_files/gallery/";
if(isset($_POST['file']) && is_array($_POST['file']))
{
foreach($_POST['file'] as $file)
{

$dirname = $path.$file;

//Recursive Delete Function

function DELETE_RECURSIVE_DIRS($dirname)
{ // recursive function to delete
// all subdirectories and contents:
if(is_dir($dirname))$dir_handle=opendir($dirname);
while($file=readdir($dir_handle))
{
if($file!="." && $file!="..")
{
if(!is_dir($dirname."/".$file))unlink ($dirname."/".$file);
else DELETE_RECURSIVE_DIRS($dirname."/".$file);
}
}
closedir($dir_handle);
rmdir($dirname);
return true;
}

}
}
?>

i code is from here
http://www.php.happycodings.com/File_Manipulation/code44.html

djr33
03-16-2011, 03:05 PM
My code might work (in the link from Bluewalrus) but I didn't test it and it has no error correction. There are some more versatile functions here:
http://php.net/manual/en/function.rmdir.php

One of those should definitely work.

It sounds to me like you might have file permissions problems. Are you on a linux server? Do you know how to use chmod to change the permissions for 777 or 755? (Note: you must do this for this directory and all directories/files inside it. Most FTP programs have an option for that.)

dsjoes
03-16-2011, 03:17 PM
i have tryed yours and it is still the same my folders are set at 755 i have checked on my host that they are at 755

i used this code i wrote to create them

$file = $_GET['folder'];
mkdir("../../gallery2.0/gallery_files/gallery/$file", 0755);
echo "<br /><br /><center><h1>Folder created</h1></center>";

i can delete the folder and images in the first folder with the script in my first post it is just the since i have added a folder inside called thumbnails that i can't delete it.

djr33
03-16-2011, 03:21 PM
Are you sure that the thumbnails folder is 755 too (and all the files within it)? If it's working otherwise, that's the obvious answer. I'm not sure what else would be wrong.

dsjoes
03-16-2011, 03:26 PM
yes the folder is 755 but it is empty at the minute because i haven't finished the thumbnail creation script yet

djr33
03-16-2011, 03:40 PM
Does it help if you set it to 777?

dsjoes
03-16-2011, 03:54 PM
still the same

if i do this to the code in my first post it works because the thumbnail folder is empty but i get error messages because it trys to unlink the folder first and it then trys to rmdir the pictures.


<?php
$path = "../../gallery2.0/gallery_files/gallery/";
if(isset($_POST['file']) && is_array($_POST['file']))
{
foreach($_POST['file'] as $file)
{

$mydir = $path.$file;
$d = dir($mydir);
while($entry = $d->read()) {
if ($entry!= "." && $entry!= "..") {
unlink($mydir."/".$entry);
rmdir($mydir."/".$entry); // i added this bit
}
}
$d->close();
rmdir($mydir);

}
}
?>


Warning: unlink(../../gallery2.0/gallery_files/gallery/untitled/thumbnails) [function.unlink]: Is a directory in /hermes/bosweb/web230/b2302/ipg.removed/test_server/admin/gallery/index.php on line 60

Warning: rmdir(../../gallery2.0/gallery_files/gallery/untitled/test.jpg) [function.rmdir]: No such file or directory in /hermes/bosweb/web230/b2302/ipg.removed/test_server/admin/gallery/index.php on line 61

bluewalrus
03-16-2011, 05:50 PM
In post 4 you've only set up the function. You never actually call it. To call a function you put its name and the value(s) you're passing it in parenthesis. So add this


DELETE_RECURSIVE_DIRS($path);

to post 4.

dsjoes
03-16-2011, 10:21 PM
i get the below errors and it has deleted pictures out of first folder

Warning: unlink(../../gallery2.0/gallery_files/gallery/folder/thumbnails) [function.unlink]: Is a directory in /hermes/bosweb/web230/b2302/ipg.removed/test_server/admin/gallery/index.php on line 60

Warning: rmdir(../../gallery2.0/gallery_files/gallery/folder/thumbnails) [function.rmdir]: Directory not empty in /hermes/bosweb/web230/b2302/ipg.removed/test_server/admin/gallery/index.php on line 61

Warning: rmdir(../../gallery2.0/gallery_files/gallery/folder/pic.jpg) [function.rmdir]: No such file or directory in /hermes/bosweb/web230/b2302/ipg.removed/test_server/admin/gallery/index.php on line 61

Warning: rmdir(../../gallery2.0/gallery_files/gallery/folder) [function.rmdir]: Directory not empty in /hermes/bosweb/web230/b2302/ipg.removed/test_server/admin/gallery/index.php on line 65

this is the script

<?php
$path = "../../gallery2.0/gallery_files/gallery/";
if(isset($_POST['file']) && is_array($_POST['file']))
{
foreach($_POST['file'] as $file)
{

$dirname = $path.$file;

//Recursive Delete Function

function DELETE_RECURSIVE_DIRS($path) //line 60
{ // recursive function to delete line 61
// all subdirectories and contents:
if(is_dir($dirname))$dir_handle=opendir($dirname);
while($file=readdir($dir_handle))
{ //line 65
if($file!="." && $file!="..")
{
if(!is_dir($dirname."/".$file))unlink ($dirname."/".$file);
else DELETE_RECURSIVE_DIRS($dirname."/".$file);
}
}
closedir($dir_handle);
rmdir($dirname);
return true;
}

}
}
?>

bluewalrus
03-20-2011, 07:26 AM
That's still the same thing. You still are only declaring/setting up the function.

Try this

example 1


$value = "I'm a value";


example 2



$value = "I'm a value";
echo $value; //DELETE_RECURSIVE_DIRS($path); <--replace with this in your other code, this is only as an example. echo, and others are php functions

dsjoes
03-20-2011, 10:10 PM
i have got this working and it is called from a separate script

thanks