Tuesday 24 March 2015

How to delete server file using file making date via FTP?

<?php
// connect
$ftp = ftp_connect("server address");
if (!$ftp) die('could not connect.');

// login
$r = ftp_login($ftp, "username", "password");
if (!$r) die('could not login.');

// enter passive mode
$r = ftp_pasv($ftp, true);
if (!$r) die('could not enable passive mode.');

// get listing
$r = ftp_rawlist($ftp, "file path");
//var_dump($r);
//echo '<pre>'.var_dump($r).'</pre>';

$be4_7day=date('d-m-Y',strtotime('-7 days'));
for ($i = 0 ; $i < 100 ; $i++){
        //echo "<li>" . substr($r[$i],1) . "</li>";
        $string = substr($r[$i],1);
        $string = explode(' ', $string);
        $exact_file=end($string);
        $buff = ftp_mdtm($ftp, $exact_file);
        //echo "$exact_file was last modified on : " . date("F d Y H:i:s.", $buff).'<br>';
        $file_time=date('d-m-Y',$buff);
        $specify_dt=$be4_7day;
        if(strtotime($file_time) < strtotime($specify_dt))
        {
            //echo $exact_file.'-'.$file_time."<br>";
            ftp_delete($r, $exact_file);   
                   
        }
}
    ftp_close($ftp);

?>

No comments:

Post a Comment