Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jun 2005
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Delete All Contents of Directory BUT DON'T Delete the directory.

    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.
    PHP Code:

    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!
    Last edited by peterallcdn; 07-10-2005 at 03:17 AM.

  • #2
    New to the CF scene
    Join Date
    Jun 2005
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?
    Thanks....
    PHP Code:
    <?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');
    ?>

  • #3
    New to the CF scene
    Join Date
    Jun 2005
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Problem solved.

    Replaced:
    PHP Code:
    ftp_rmdir($conn_id$dst_dir); // delete empty directories 
    With:
    PHP Code:
    if (strpos($dst_dir'/') !== false){
         
    ftp_rmdir($conn_id$dst_dir); // delete empty directories



  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •