亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何避免PHP / WordPress內存在大文件上傳中批量處理數據的致命錯誤?

如何避免PHP / WordPress內存在大文件上傳中批量處理數據的致命錯誤?

PHP
婷婷同學_ 2022-09-03 15:57:23
我有一個大的CSV文件,我正在上傳到WordPress儀表板以導入分類術語。我寫了一個小插件,它使用函數來插入每個術語,但是,該函數緩存了大量數據以檢查slugs的唯一性和父術語依賴性,盡管將內存分配增加到0.5 Gb,但該進程的內存不足了大約1000個術語。wp_insert_term()我一直想將文件拆分為可管理的塊,以便批量處理數據并運行限制為1000或數據行的會話,這樣每個進程都會干凈利落地終止。我一直在尋找這樣的解決方案,并發現了這篇有趣的文章,內容涉及批量圖像導入面臨的類似問題,它概述了開發人員如何使用javascript通過向服務器和可管理塊發送ajax請求來控制批處理過程。它給了我一個想法,即在上傳時讀取CSV文件,逐行讀取它,并向服務器發送ajax請求以處理可管理的行數。這是實現這一目標的更好方法嗎?
查看完整描述

1 回答

?
揚帆大魚

TA貢獻1799條經驗 獲得超9個贊

我根據問題中的鏈接和一些額外的修補開發了以下解決方案。


在WordPress服務器端,當加載javascript文件時,我根據內存分配來確定服務器可以處理的行數,


$limit = ini_get('memory_limit');

$limit = wp_convert_hr_to_bytes($limit) / MB_IN_BYTES; //in MBs.

switch(true){

    case $limit >= 512:

        $limit = 1000;

        break;

    default:

        $limit = 500;

        break;

}

wp_enqueue_script( 'my-javascript-file');

wp_localize_script( 'my-javascript-file', 'cirData', array(

    'limit'=>$limit

));

您應該根據自己的過程確定并設置自己的限制。


在javascript文件中,使用jQuery,


var reader,formData, lineMarker=0, csvLines, isEOF=false, $file, $form ;

  $(document).ready(function(){

    $file = $(':file'); //file input field

    $form = $('form');  //form

    //when the file field changes....

    $file.on('change', function(){

      //check if the file field has a value.

      if($file.val()){

        //setup file reader.

        reader = new FileReader();

        //now listen for when the file is ready to be read.

        reader.addEventListener('load', function (e) {

          csvLines = e.target.result.split("\n");

          batchProcess(); //launch process.

        });

        //when the form is being submitted, start reading the file.

        $(document).on('click', ':submit', function(e){

          e.preventDefault(); //disable normal submit.

          //setup data for the ajax.

          formData = new FormData($form.get(0));


          //read the file and batch request to server.

          reader.readAsBinaryString($file.get(0).files[0]);

        })

      }

    })

  });


  // Methods

  //posting

  function postCSVdata(csvdata){

    formData.set('csvlines', csvdata); //set the current datat to send.

    $.ajax({

      type: 'POST',

      url: $form.attr('action'),

      data: formData,

      contentType: false,

      processData: false,

      cache: false,

      success: function(data){

        var msg ="";

        if(isEOF){ //is this end of the file?

          console.log("success!");

        }else{ //continue reading file.

          console.log("uploaded:"+ Math.round(lineMarker/csvLines.length*100)+"%");

          batchProcess(); //process the next part of the file.

        }

      }

    })

  }

  //batch process.

  function batchProcess(){

    //csvlines is the array containing all the lines read from the file.

    //lineMarker is the index of the last line read.

    var parsedata='', stop = csvLines.length - lineMarker, line='';


    for(var i = 0; i < stop; i++) {

      line = csvLines[i+lineMarker];

      parsedata +=line+"\n"; //add a new line char for server to process.

      //check if max limit of lines server can process is reached.

      if(i>(cirData.limit-2)) break; //batch limit.

    }

    lineMarker += i;

    if(i==stop) isEOF = true;

    postCSVdata(parsedata); //send to server.

  }

這將以服務器能夠處理的行塊的順序方式發送多個 AJAX 請求,而不會出現致命的內存錯誤。


查看完整回答
反對 回復 2022-09-03
  • 1 回答
  • 0 關注
  • 120 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號