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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.