class FtpGet {
    private $config;
    private $retry = 15;
    private $errors = array();
    
    function __construct($config = array()) {
        if (empty ( $config )) {
            $this->config = array (
                 ‘path_local’ => ‘/local/path’,
                 ‘path_remote’ => ‘/remote/path’,
                 ’server’ => ‘ftp.server.com’,
                 ‘username’ => ‘username’,
                 ‘password’ => ‘password’
            );
        } else {
            $this->config = $config;
        }
    }
    
    public function getFile($filename) {
        if (empty ( $filename ))
            return “”;
        $this->errors = array();
        $return_file = “”;
        $i = 0;
        while ( $i < $this->retry ) {
            try {
                //try to connect
                $conn = ftp_connect ( $this->config ['server'], 21 );
                if (! $conn) {
                    $this->errors['connect'] = “can not connect to the ftp server ” . $this->config ['server'];
                    sleep ( 30 );
                    $i ++;
                    continue;
                }
                
                //try to login
                $lr = ftp_login ( $conn, $this->config ['username'], $this->config ['password'] );
                if (! $lr) {
                    $this->errors['login'] =  “can not login to the ftp server ” . $this->config ['server'];
                    sleep ( 30 );
                    $i ++;
                    continue;
                }
                
                //check local path    
                if (! $this->checkPath ( $this->config ['path_local'] )) {
                    $this->errors['path'] =  “can not create local path {$this->config ['path_local']}, please chmod the dir to 777.”;
                    break;
                }
                
                //set to pasv mode
                if(!ftp_pasv ( $conn, true )){
                    $this->errors['pasv'] =  “can not set transfer mode to pasv.”;
                    break;
                }

                //get remote files list
                $fileList = ftp_nlist ( $conn, $this->config ['path_remote'] );
                if (count ( $fileList ) === 0) {
                    $this->errors['nofile'] =  “there is not files on the ftp server ” . $this->config ['server'];
                    sleep ( 600 );
                    $i ++;
                    continue;
                }
                
                //loop to check if matched the file
                foreach ( $fileList as $single_file ) {
                    if ((preg_match ( “/^(.*?)$filename\$/i”, $single_file )) && ftp_size ( $conn, $single_file ) > 0) {
                        $local_file = $this->config ['path_local'] . “/” . basename ( $filename );
                        $rs = ftp_get ( $conn, $local_file, $single_file, FTP_BINARY );
                        if ($rs) {
                            $return_file = $single_file;
                        } else {
                            $this->errors['nofile'] = “can not get the file ” . $single_file;
                        }
                        break;
                    }
                }
                
                //close ftp connection
                $this->errors['close'] = ftp_close ( $conn );
            
            } catch ( Exception $e ) {
                $this->errors['exception'] = “found error: {$e->getMessage()}\n”;
                sleep(5);
            }
            if (! empty ( $return_file )) {
                break;
            }
            
            $i ++;
        }
        
        if (empty($return_file) && count($this->errors) > 0) {
            $title = “[Error] Could not get file {$filename} from ftp server {$this->config['server']}”;
            $message = implode(”\n”, $this->errors);
            $this->sendMail ( $title, $message );
        }
        return $return_file;
    }
    private function checkPath($Path) {
        if (! is_dir ( $Path ) || ! file_exists ( $Path )) {
            if (mkdir ( $Path, 0777 )) {
                return 1;
            } else {
                return 0;
            }
        } else {
            return 1;
        }
    }
   
    private function sendMail($title, $msg) {
        $boundary = uniqid ();
        $mail_header = “From: abc@from.com\n”;
        $mail_header .= “Content-type: multipart/related; boundary=\”{$boundary}\”\n”;
       
        $mail_body = “\n–{$boundary}\n”;
        $mail_body .= “Content-type: text/plain; charset=\”iso-8859-1\” \n”;
        $mail_body .= “Content-transfer-encoding: 7bit \n\n”;
        $mail_body .= “This is a auto mail from process download file from ftp server.\n”;
        $mail_body .= “{$msg}\n\n”;
       
        $mail_body .= “–{$boundary}–\n”;
       
        $mail_to = “haha@email.com”;
        if (mail ( $mail_to, $title, $mail_body, $mail_header )) {
            echo “Send alert email successfully ! \n”;
        }
    }
}