Remove Files

Discussion in 'PHP' started by Weirfire, Apr 13, 2007.

  1. #1
    Is there a function in PHP which will clear out all the files in a specified folder?

    If there is no global solution I will use ftp_delete($conn_id, $file)

    I'm looking for a function which will clear out a folder daily with a CRON job.
     
    Weirfire, Apr 13, 2007 IP
  2. Nikolas

    Nikolas Well-Known Member

    Messages:
    1,022
    Likes Received:
    22
    Best Answers:
    0
    Trophy Points:
    150
    #2
    You should parse the dir with opendir() and delete files with unlink()
     
    Nikolas, Apr 13, 2007 IP
    Weirfire likes this.
  3. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #3
    Thanks for the suggestion.

    This is the solution I went with;


    <?php
    
    // set up basic connection
    $conn_id = ftp_connect('ftp.domain.com');
    
    // login with username and password
    $login_result = ftp_login($conn_id, '<username>', '<password>');
    
    
    $folder = '/public_html/directory/';
    
    $contents = ftp_nlist($conn_id,$folder);
    for($i=2;$i<count($contents);$i++){
    	ftp_delete($conn_id,$folder . $contents[$i]);
    }
    // close the connection
    ftp_close($conn_id);
    ?>
    PHP:
     
    Weirfire, Apr 13, 2007 IP