tobeyt23 Posted June 29, 2011 Share Posted June 29, 2011 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 More sharing options...
trq Posted June 29, 2011 Share Posted June 29, 2011 I would think reading the file into a string using file_get_contents() would likely be a more efficient method. Link to comment https://forums.phpfreaks.com/topic/240703-performancememory-help/#findComment-1236338 Share on other sites More sharing options...
tobeyt23 Posted June 29, 2011 Author Share Posted June 29, 2011 I do that in the parse function $data = file_get_contents($file);. Do you mean somewhere else? Link to comment https://forums.phpfreaks.com/topic/240703-performancememory-help/#findComment-1236392 Share on other sites More sharing options...
trq Posted June 29, 2011 Share Posted June 29, 2011 Yes, to get the actual file. Link to comment https://forums.phpfreaks.com/topic/240703-performancememory-help/#findComment-1236564 Share on other sites More sharing options...
tobeyt23 Posted June 29, 2011 Author Share Posted June 29, 2011 Not sure I am following you. I download and save the if it doesn't exist. Then I do file_get_contents() on the file. What are you saying I should be doing differently? Link to comment https://forums.phpfreaks.com/topic/240703-performancememory-help/#findComment-1236570 Share on other sites More sharing options...
trq Posted June 30, 2011 Share Posted June 30, 2011 Use file_get_contents() to get the file. Link to comment https://forums.phpfreaks.com/topic/240703-performancememory-help/#findComment-1236655 Share on other sites More sharing options...
tobeyt23 Posted June 30, 2011 Author Share Posted June 30, 2011 Nope, tried with same results. Link to comment https://forums.phpfreaks.com/topic/240703-performancememory-help/#findComment-1236774 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.