Good day!
There is a txt file with a large number of lines, a document size of 15 - 30 mb.
Problem:
When there are not many lines in the file, everything works as expected, but in the case when we have a large number of lines, we get an error:
Fatal error: Allowed memory size of 268435456 by exhausted (tried to allocate 72 bytes).
How best to get around this problem, it comes to mind to put a counter and load data for example by n number of rows. I would like to know what solutions you use.
PHP function with which I open upload the file to the server, read the lines and break them into an array:
function file_processing ($upload_dir, $file) { // File propertie $file_name = $file['name']; $file_tmp = $file['tmp_name']; $file_size = $file['size']; $file_error = $file['error']; // Work out the file xxtention $file_ext = explode('.', $file_name); $file_ext = strtolower(end($file_ext)); $allowed = array('txt'); if (in_array($file_ext, $allowed)) { if ($file_error === 0) { $file_name_new = $file_name; $file_destination = $upload_dir . '' . $file_name_new; if (move_uploaded_file($file_tmp, $file_destination)) { $file = fopen($file_destination, "r") or die ("Unable to open file!"); $fp = file($file_destination); $array = []; foreach ($fp as $key) { $tmp_mass = explode("\";\"", $key); array_push($array, $tmp_mass); } fclose($file); unlink($file_destination); return $array; } else { echo "Please select only text file (.txt file is recomendet)!"; } } } } The function that loads the data in the mysql database:
function post_integration($arr, $array) { global $wpdb; $cat = $array['category']; $manufacturer = $array['manufacturer']; $return_status = $array['return_status']; $markup = $array['markup']; for ( $i = 1; $i < count($arr); $i++ ) { $vendor_code = ( iconv ("CP1251", "UTF-8", $arr[$i][0]) ); $vendor_code = substr($vendor_code, 1); $product_name = ( iconv ("CP1251", "UTF-8", $arr[$i][1]) ); $price = ( iconv ("CP1251", "UTF-8", $arr[$i][2]) ); $rabat_group = ( iconv ("CP1251", "UTF-8", $arr[$i][3]) ); $product_desc = ( iconv ("CP1251", "UTF-8", $arr[$i][4]) ); $mortgage_value = ( iconv ("CP1251", "UTF-8", $arr[$i][5]) ); $netto_weight = ( iconv ("CP1251", "UTF-8", $arr[$i][6]) ); $netto_weight = substr($netto_weight, 0, -1); $price = str_replace(",",".",$price); $this_price = ( ($markup / 100) * $price) + $price; $end_price = round($this_price, 2); $query="SELECT vendor_code.meta_value AS \"vendor_code\", p.ID AS \"ID\" FROM wp_posts p JOIN wp_postmeta vendor_code ON p.ID = vendor_code.post_id AND vendor_code.meta_key = 'vendor_code' WHERE post_status = 'publish' AND post_type = 'post' AND vendor_code.meta_value = $vendor_code"; $rabrt_id_query = "SELECT id FROM `wp_rabat_list` WHERE name_rabat = '$manufacturer'"; $result_data = $wpdb->get_results($query); $rabat_data = $wpdb->get_results($rabrt_id_query); $rabat_id = $rabat_data[0]->id; $result_id = $result_data[0]->ID; $result_vendor = $result_data[0]->vendor_code; if(isset($result_vendor)) { $my_post = array(); $my_post['ID'] = intval($result_id); $my_post['post_title'] = wp_strip_all_tags($product_name); $my_post['post_content'] = $product_desc; $my_post['post_category'] = array($cat); wp_update_post($my_post); update_post_meta($my_post['ID'], 'vendor_code', $vendor_code); update_post_meta($my_post['ID'], 'price', $end_price); update_post_meta($my_post['ID'], 'manufacturer', $manufacturer); update_post_meta($my_post['ID'], 'mortgage_value', $mortgage_value); update_post_meta($my_post['ID'], 'netto_weight', $netto_weight); // update_post_meta($my_post['ID'], 'availability', $article); update_post_meta($my_post['ID'], 'return_goods', $return_status); update_post_meta($my_post['ID'], 'discount_group_id', $rabat_id); update_post_meta($my_post['ID'], 'mark_up', $markup); } else { $post_data = array( 'post_title' => wp_strip_all_tags( $product_name ), 'post_content' => $product_desc, 'post_status' => 'publish', 'post_author' => 1, 'post_category' => array($cat) ); //Вставляем запись в базу данных $post_id = wp_insert_post( $post_data ); add_post_meta($post_id, 'vendor_code', $vendor_code); add_post_meta($post_id, 'price', $end_price); add_post_meta($post_id, 'manufacturer', $manufacturer); add_post_meta($post_id, 'mortgage_value', $mortgage_value); add_post_meta($post_id, 'netto_weight', $netto_weight); // add_post_meta($post_id, 'availability', $article); add_post_meta($post_id, 'return_goods', $return_status); add_post_meta($post_id, 'discount_group_id', $rabat_id); update_post_meta($my_post['ID'], 'mark_up', $markup); } } }