Jump to content

[SOLVED] batch file deleting and folder deleting needed due to user problem


johlwiler

Recommended Posts

Hi all,

 

I have a folder in my web site where I used php to create and copy in files. but now I can't delete them easily. The only way I know how that is possible is through php. I get an error the following ftp error:

550 th: Permission denied

I had the chmod() set to 0777 on every file and folder. But I still seem to loss control over the files when I try and do things with them through other means.

 

I think it is because the user I use to access the files via ftp a diffrent user. So I tryed to add a function with chown() for the new files I was posting and uploading. It didn't work on those new files. so it looks like the only thing I can do is use the unlink() function which deletes the files fine.

 

The only problem is now I have something like 100 sub directories all with files in them. I need to figure out a way to batch delete them. I can't find a script that goes through and finds all file contents including all sub directories and unlink those things.

 

I found some that will use a for loop to list out the contents of a folder but it is just files. No sub directories.

 

Any body got any ideas on which functions I should use or know where a script is that I can base mine off of.

 

I am not sure how to approach this since what I tryed with chown and chmod has failed thus far.

 

I currently have no code and am starting from scratch on this batch flushing of my directory.

Link to comment
Share on other sites

I can't ssh in just ftp. If I did and could su root I wouldn't have a problem at all.

So I think my only option is php.

 

I think I see what jscix is saying

 

open the directory

then read all contents

from there I guess I can find a way to unlink them

then close the directory

the only thing is here how would I deal with sub directories

 

I have a file propimg then under that I have folders with different contents under different naming conventions. then most of those folders have sub folders. Total it is like 700 different files and folders.

 

Am I going to have to go one by one through them all??

 

Thats what I am trying to figure out.

Link to comment
Share on other sites

This has not been tested well, im sure there are tons of errors.. anyway. I hope it helps you.

 

<?php

function DelTree($start_Directory) {
$directory_List = array();
echo "<b>Removing all contents from: " . $start_Directory . "</b><br>";

if ($handle = opendir($start_Directory))
{
while (false !== ($curEle = readdir($handle)))
  {
   if (is_dir($curEle) && substr($curEle,0,1) !== ".")
   {
   array_push($directory_List,$curEle);
   } 
   else
     {
      echo "unlink file: " . $curEle . "<br>";
     }
  }
  closedir($handle);
}

foreach ($directory_List as $dir)
  {
      $new_Dir = $dir;
      array_shift($directory_List);
      DelTree($new_Dir);
  }

}

DelTree("/var/www/");



?>

Link to comment
Share on other sites

ok

I tryed rarebits code idea first and got some hang ups so I moved on to jscix

 

got his to respond fine with out the unlink function. it echoed out all the directories. So right below the echo I add unlink to the and the same varible and got a loop of errors instead. Basically telling my that the files and dir do not exists.

Warning: unlink(1): No such file or directory in /home/worldproperties/www/worldpropertiesonline.net/delete_all.php on line 18

unlink file: 4

 

here is the code

 


<?php

function DelTree($start_Directory) {
$directory_List = array();
echo "<b>Removing all contents from: " . $start_Directory . "</b><br>";

if ($handle = opendir($start_Directory))
{
while (false !== ($curEle = readdir($handle)))
  {
   if (is_dir($curEle) && substr($curEle,0,1) !== ".")
   {
   array_push($directory_List,$curEle);
   } 
   else
     {
      echo "unlink file: " . $curEle . "<br>";
  unlink($curEle); //added by me to unlink the files.
     }
  }
  closedir($handle);
}

foreach ($directory_List as $dir)
  {
      $new_Dir = $dir;
      array_shift($directory_List);
      DelTree($new_Dir);
  }

}

DelTree("/home/worldproperties/www/worldpropertiesonline.net/propimg");



?>

Link to comment
Share on other sites

ok in the unlink I added the $start_Directory variable.

 

  $deldir=$start_Directory."/".$curEle;

  unlink($deldir);

 

And it lopped through with this error which is making me think I need to call my host unless I am missing something.

 

Warning: unlink(/home/worldproperties/www/worldpropertiesonline.net/propimg/1): Operation not permitted in /home/worldproperties/www/worldpropertiesonline.net/delete_all.php on line 19

unlink file: 4

 

is the error I am getting except it is looping through on every file.

 

operation not permitted.

 

this is getting very weird.

Link to comment
Share on other sites

That is happening because on each loop through $start_Directory is getting replaced by the current directory that it's in,

 

you need to use a full path name for the unlink;

 

on a side-note: I'm sorry - I should of mentioned, never run something that deletes files in that way, without first making sure it's going to do EXACTLY, what you want it to.

Link to comment
Share on other sites

I figured out the problem you can't unlink dir you have to use rmdir() so I built an if statement that showed if it was a dir or file and either unlinked it or opened it to delete what was in it and then unlinked it. Thanks for everyones input. jscix you headed me right where I needed. Thanks

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.