Delete Server files from another server using php via ftp connection

Delete Server files from another server using php via ftp connection

The below function used to delete Server file from another server using php via ftp connection. This will helpfull to delete the server file. just konw about ftp server address,username, password. and file path.

The below function delete the file from server greater then 7 days, ftp_mdtm("connection","filename"); used to find the file making date.

ftp_rawlist('connection','filepath'); find the list of file from given path.

 

// 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);

0 Comments

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed

>