Loop a TXT file under a folder where the data is read by row into the database appears Fatal error:allowed memory size of 134217728 bytes exhausted error message,
The code below, I each TXT file is not big (about 100k), and is read by the line, why?
$file = new \FilesystemIterator($fileName); foreach ($file as $fileinfo) { if ( $fileinfo->isFile() ) { $fp = fopen($fileName . '/' . $fileinfo->getFilename(), 'r'); while(! feof($fp)) { $data['score'] = 7; $data['code'] = fgets($fp); $this->book->add($data);//存进数据库 } fclose($fp); } }
Reply content:
Loop a TXT file under a folder where the data is read by row into the database appears Fatal error:allowed memory size of 134217728 bytes exhausted error message,
The code below, I each TXT file is not big (about 100k), and is read by the line, why?
$file = new \FilesystemIterator($fileName); foreach ($file as $fileinfo) { if ( $fileinfo->isFile() ) { $fp = fopen($fileName . '/' . $fileinfo->getFilename(), 'r'); while(! feof($fp)) { $data['score'] = 7; $data['code'] = fgets($fp); $this->book->add($data);//存进数据库 } fclose($fp); } }
Iterative operation with keyword yield does not store intermediate variables to save memory and requires php5.5+
$this->book->add ($data);
Unset ($data);
While looping, $data will occur when write-time replication (COW), which will not be released until the end of your script execution
Install Xdebug or xhprof to view performance analysis reports.
I'm estimating the configuration of the framework, SQL did a collection