Jump to content

Performance/Memory Help


tobeyt23

Recommended Posts

I use the following to pull a text file from a web source and save it on my server. Then l go thru and replace the newlines and tabs so I can read it and make it into and array to save into my database table.

 

Intially I was have memory issues so I set set_time_limit(0); and ini_set('memory_limit','-1');. But it seems that my server is getting crushed whenever I run this and never returns to normal after completion.

 

Can I do this a different way to help with the performance and memory?

 

function load() {
        $data           = 'false';
        $file        = 'http://www.host.com/file';
        $localfile   = WWW_ROOT . '/files/FILE_'.date('Ymd').'.txt';
        
        if(!file_exists($localfile)) {
            if(copy($file, $localfile)) {
                $data = $this->parse($localfile);
            }
        } else {
            $data = $this->parse($localfile);
        }
        
        return $data;   
    }
    
    protected function parse($file) {
        $data       = file_get_contents($file);
        $data       = str_replace( array( "\r\n" , "\t" ) , array( "[NEWLINE]" , "[TAB]" ) , $data );
        $_lines     = explode("[NEWLINE]" , $data);
        $_data      = array();
                
        for($l=1; $l<count($_lines); $l++) {
            $_field = explode("[TAB]", $_lines[$l]);
            
            for($f=0; $f<count($_field); $f++) {
               switch($f) {
                    case 0: $lname = $_field[$f]; break;
                    case 1: $fname = $_field[$f]; break;
                    case 2: $mname = $_field[$f]; break;
                    case 3: $name_suffix = $_field[$f]; break;
                }
            }
            
            array_push($_data, array('lname' => $lname, 'fname' => $fname, 'mname' => $mname, 'name_suffix' => $name_suffix));
        }
        
        return $_data;
        
    }

if($ascdata) {
            $this->NewAscRecords->query("TRUNCATE TABLE new_asc_records");
            
            foreach($ascdata as $a) {
                $this->NewAscRecords->read(null);
                $this->NewAscRecords->set('id', null);
                $this->NewAscRecords->set('st_abbr', $a['st_abbr']);
                $this->NewAscRecords->set('lic_number', $a['lic_number']);
                $this->NewAscRecords->set('fname', $a['fname']);
                $this->NewAscRecords->set('lname', $a['lname']);
                $this->NewAscRecords->set('mname', $a['mname']);
                $this->NewAscRecords->set('name_suffix', $a['name_suffix']);
                $this->NewAscRecords->set('eff_date', $a['eff_date']);
                $this->NewAscRecords->set('exp_date', $a['exp_date']);
                $this->NewAscRecords->set('issue_date', $a['issue_date']);
                $this->NewAscRecords->set('lic_type', $a['lic_type']);
                $this->NewAscRecords->set('status', $a['status']);
                $this->NewAscRecords->set('company', $a['company']);
                $this->NewAscRecords->set('phone', $a['phone']);
                $this->NewAscRecords->set('street', $a['street']);
                $this->NewAscRecords->set('city', $a['city']);
                $this->NewAscRecords->set('state', $a['state']);
                $this->NewAscRecords->set('county', $a['county']);
                $this->NewAscRecords->set('county_code', $a['county_code']);
                $this->NewAscRecords->set('zip', $a['zip']);
                $this->NewAscRecords->set('aqb_compliant', $a['aqb_compliant']);
                $this->NewAscRecords->set('discipline_action', $a['discipline_action']);
                $this->NewAscRecords->set('discipline_start_date', $a['discipline_start_date']);
                $this->NewAscRecords->set('discipline_end_date', $a['discipline_end_date']);
                $this->NewAscRecords->save();
            }
        }

Link to comment
https://forums.phpfreaks.com/topic/240703-performancememory-help/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.