Monday, September 30, 2013

Php FTP Functions ( phpfox )


This is a simple function to connect to an ftp and download the xml files from that ftp to a specified folder in our server

$ftp_server = 'abcdefg.com';      // FTP Server Address (exlucde ftp://)
        $ftp_user_name = 'abcuser';     // FTP Server Username
        $ftp_user_pass = 'abcpass';      // Password
        // Connect to FTP Server
        $conn_id = ftp_connect($ftp_server);
        // Login to FTP Server
        $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
        // Verify Log In Status
        if ((!$conn_id) || (!$login_result))
        {
           Phpfox_Error::set('FTP connection has failed!Attempted to connect to'. $ftp_server.' for user'. $ftp_user_name);
        } else {
           Phpfox::addMessage('Connected to '. $ftp_server .', for user '. $ftp_user_name);
        }
        $contents = ftp_nlist($conn_id, ".");
        foreach($contents as $currentFile)
        {
            // to check directory ('0') or file('1')
            $fType =  Phpfox::getService('moduleName')->is_ftp_dir($currentFile, $conn_id);
            $ext = pathinfo($currentFile, PATHINFO_EXTENSION);
            if($ext == 'xml')
            {        
                // define some variables
// check whether the xml is already downloaded and saved in Database
                $getxmlDetailsFromDb = Phpfox::getService('moduleName')->getxmlDetailsFromDb($currentFile);
                if($getxmlDetailsFromDb['xml_name'] =='')
                {
                    $local_file = Phpfox::getParam('core.dir_file').'xml/'.$currentFile;
                    $server_file = $currentFile;
                    // try to download $server_file and save to $local_file
                    if (ftp_get($conn_id, $local_file, $server_file, FTP_BINARY))
                    {
// inserting the downloaded xml name to DB by giving ftpconnect as download type which is used for future reference.
                        $uptype = 'ftpconnect';
                        $XMLtoDB = Phpfox::getService('moduleName')->insertXMLnameToDb($server_file,$uptype);
                    }
                }
               
            }
        }
         // close the FTP connection
        ftp_close($conn_id);

// reading xml datas from DB
        $getxmlDatas = Phpfox::getService('moduleName')->getxmlDatas();
        foreach ($getxmlDatas as $keyxml => $valueXML)
        {
            // reading each xml and fetching the datas.
            $xml=simplexml_load_file(Phpfox::getParam('core.dir_file').'xml/'.$valueXML['xml_name']);
// converting the xml object to array.
            $array = Phpfox::getService('moduleName')->object_to_array($xml);
}

// by printing this '$array' we will get the xml values as
echo '<pre>';
print_r($array);
echo '</pre>';


public function object_to_array($obj)
    {
        if(is_object($obj)) $obj = (array) $obj;
        if(is_array($obj))
        {
            $new = array();
            foreach($obj as $key => $val)
            {
                $new[$key] = $this->object_to_array($val);
            }
        }
        else $new = $obj;
        return $new;      
    }


Reference : php.net

No comments:

Post a Comment