...

View Full Version : Delete All Contents of Directory BUT DON'T Delete the directory.



peterallcdn
07-09-2005, 12:49 PM
Hello,

I want to FTP into one of my sites from another of my sites and recursively delete all files and directories in the /public_html/ folder, BUT I don't want to delete the public_html folder itself.

This script does all the recursive deleting. When I call the function, I tell it the directory I want emptied "ftp_rmAll($conn_id, '/public_html');". The only problem is that when it's done, it deletes the public_html folder also.



See Modified Script In Next Post.....


Does anyone know of a way to alter this script so it will stop before deleting public_html ?

EDIT: This script appears not to recursively delete folders correctly, although it does files. It leaves empty folders which have other empty folders inside them.

Thanks for any help!

peterallcdn
07-10-2005, 02:17 AM
The script actually does seem to recurse correctly, it was some hidden .htaccess files that stopped it for some reason... But anyways.....

Does anyone know how to stop this script from deleting public_html? I want every file and folder inside of it to be deleted, but keep public_html itself.

Anyone? :rolleyes:
Thanks....


<?php
$ftp_server = 'domain.com';
$ftp_user = 'username';
$ftp_password = 'password';
$conn_id = ftp_connect($ftp_server);
$login_result = ftp_login($conn_id, $ftp_user, $ftp_password);

function ftp_rmAll($conn_id,$dst_dir)
{
$ar_files = ftp_rawlist($conn_id, $dst_dir);
if (is_array($ar_files)) { // makes sure there are files
foreach ($ar_files as $st_file) { // for each file
if (ereg("([-d][rwxst-]+).* ([0-9]) ([a-zA-Z0-9]+).* ([a-zA-Z0-9]+).* ([0-9]*) ([a-zA-Z]+[0-9: ]*[0-9]) ([0-9]{2}:[0-9]{2}) (.+)",$st_file,$regs)) {

// ADDITION TO SKIP CURRENT AND PARRENT FOLDERS
if (($regs[8] == ".") || ($regs[8] == "..")) {
continue; }

if (substr($regs[1],0,1)=="d") { // check if it is a directory
ftp_rmAll($conn_id, $dst_dir."/".$regs[8]); // if so, use recursion
} else {
ftp_delete($conn_id, $dst_dir."/".$regs[8]); // if not, delete the file
}
}
}
}
ftp_rmdir($conn_id, $dst_dir); // delete empty directories
}
ftp_rmAll($conn_id, 'public_html');
?>

peterallcdn
07-10-2005, 07:25 AM
Problem solved.

Replaced:

ftp_rmdir($conn_id, $dst_dir); // delete empty directories
With:

if (strpos($dst_dir, '/') !== false){
ftp_rmdir($conn_id, $dst_dir); // delete empty directories
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum