Jump to content
xisto Community
Sign in to follow this  
AnkitGoswami

Can't Delete A Folder On Server! a small mistake in a script....

Recommended Posts

I maintain my college's website and since I update it frequently, I decided to write?a?php?script?that?would?backup?all?the?data?in?a?folder?on?the?server?itself.?To?backup?all?the?data?I?would?only?need?to?run?the?script?once?in?a?while:


 

{

 

$path = $source . '/' . $file;

 

if(is_file($basePath . $path))

 

{

 

print 'File ('.$path.')

';

 

if(!is_file($basePath . $dest . '/' . $file) || $overwrite)

 

if(!@copy($basePath . $path, $basePath . $dest . '/' . $file))

 

{

 

print 'File ('.$path.') not copied.';

 

}

 

}

 

elseif(is_dir($basePath . $path) && $path!="/backup")

 

{

 

print 'Dir ('.$path.')

';

 

if(!is_dir($basePath . $dest . '/' . $file))

 

mkdir($basePath . $dest . '/' . $file);

 

dircpy($basePath, $path, $dest . '/' . $file, $overwrite);

 

}

 

}

 

}

 

closedir($handle);

 

}

 

}

 

print dircpy("","", "backup/", $overwrite = false);

 

?>

_linenums:0'><?function dircpy($basePath, $source, $dest, $overwrite = false){print "============================================================================"; if(!is_dir($basePath . $dest)) mkdir($basePath . $dest); if($handle = opendir($basePath . $source)) { while(false !== ($file = readdir($handle))) { if($file != '.' && $file != '..') { $path = $source . '/' . $file; if(is_file($basePath . $path)) { print 'File ('.$path.') '; if(!is_file($basePath . $dest . '/' . $file) || $overwrite) if(!@copy($basePath . $path, $basePath . $dest . '/' . $file)) { print 'File ('.$path.') not copied.'; } } elseif(is_dir($basePath . $path) && $path!="/backup") { print 'Dir ('.$path.') '; if(!is_dir($basePath . $dest . '/' . $file)) mkdir($basePath . $dest . '/' . $file); dircpy($basePath, $path, $dest . '/' . $file, $overwrite); } } } closedir($handle); }}print dircpy("","", "backup/", $overwrite = false);?>

?

 

What the script does is copy all the files (including all the files in the subfolders) to the backup folder using recursion. The first time I ran this script I forgot one thing :

elseif(is_dir($basePath . $path) && $path!="/backup")

I did not put the second condition in the above elseif.

Now you might be beginning to understand my problem. The script went into a mad loop and started creating backup/backup/backup/backup/backup/backup....... Now the folders run soo deep that they have exhausted my webspace and I?am?unable?to?delete?them?('cause of 3 things : first, the folders run too deep, second, they contain .ftpquota files and third, I cannot upload any more files to try and run a script to delete them).

 

Can?anyone?think?of?a?solution??

Share this post


Link to post
Share on other sites

First I would chmod the php script so it can't be executed by setting the chmod to 000, then I would download a ftp software and that way you can select all those files for a mass delete. Of course I am assuming that that your back ups under a folder one folder so it should make it rather easy to do, if not just delete the files through a FTP software and you should be set :). Once completed delete your old php script and then upload the one you really need.

Share this post


Link to post
Share on other sites

First, i recommend using SM's suggestion of chmodding the script to 000, then:Well if it's a ftp manager that's refusing to delete them, try using cpanel filemanager and just delete that one folder. Everything should go at once :)The cpanel will delete it easier since it's on the server, and hence will do it much faster too!or thirdly, if you want to upload a script to delete them, after chmodding the original script to 000, save a couple of the college's less important files to your computer and then delete them from the server. This should give you enough room to upload a delete script...BUT be very careful with a delete script. Something may go wrong and clean out your college's webserver, which would cause a lot of annoyances...

Edited by Jimmy (see edit history)

Share this post


Link to post
Share on other sites

the trouble is they?don't have cpanel but plesk and I hate it.?I can't figure out how to use the file manager so I will have to resort to?downloading?a?lot?of?data?and?then?deleting?that?on?the?server?to?make?enough?room?for?uploading?files.Then?I?can?run?a?delete?script?and?also?change?the?backup?script.?The?real?problem?is?the?site?has?gone?way?over?the?limit (guess the size restriction doesn't apply when you are creating files using php) so I'll have to download a lot and downloading?files from ftp is soo slow.?

Share this post


Link to post
Share on other sites

Is it possible that the php script is being run under a different user than yourself, so you do not have the authority to delete them?

Share this post


Link to post
Share on other sites

well it is not that I can't delete the folder but whenever I try to delete it the ftp client (or explorer) hangs because of the many many folders that were created inside it. It's like backup/backup/backup/backup/backup/backup/backup.....? ?so the ftp client gets stuck just trying to figure out the time required for deletion.Go ahead and try the script in localhost without the $path!="/backup" inelseif(is_dir($basePath . $path) && $path!="/backup")put?the?php?file?in?any?folder?and?give?the?arguments?like(in the last line):?dircpy("","../", "backup/", $overwrite = false);

Share this post


Link to post
Share on other sites

When you create a folder through a script, you lose permission to that folder because it is given the rights to the group "www-data" and not you. The only way to delete that folder is by having a script do it. But the PHP script can only delete folders that are empty.

Share this post


Link to post
Share on other sites

I took care of it last night. Yeah, if?you?directly?use?the?rmdir?function?in?php?it?will?not?be?able?to?remove?non?empty?folders?so?i?used?unlink?for?all?the?files ?present and then rmdir (using a modified version of the script above).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.