0

Currently doing upload to database from csv file. now after looping 2k rows of data that takes too long to wait. then after trying to cancel via ajax request with

        $.each(xhrPool, function(idx, jqXHR) {
            jqXHR.abort();
        });

ajax would be aborted, but then the php process still keeps going even if I refresh the page the still process import dont stop. it will only load the page after finishing the upload. is there a way to abort php function from front end. but no other link work with my page when doing upload it will only work after the upload

my import file of PHP

        while ( ($data = fgetcsv($file, 30000, ",")) !==FALSE ){
            foreach row insert to database;
        }

        fclose($file);
kaks
  • 27
  • 6
  • 2
    Maybe you can think of a different (and maybe better solution) 1) only pull 2k rows with your query/ajax request. 2) build a counter into your while that stops after 2k rows. – Refilon Jun 27 '19 at 07:41
  • Yeah, I guess that is also exactly what im doing right now, but is there a way to cancel this foreach? example client dont want to wait that long so they decided to cancel it, but in my case only ajax has been cancelled not the php function – kaks Jun 27 '19 at 07:46
  • As far as I know there two main ways. The first is to check some flag in every iteration. So in your while loop you select some flag from a database or a file and if it false stop the loop. In ajax query for abort you just change this flag value to false. The second is to create loop on js side which will be send queries with count of string that already processed. – marv255 Jun 27 '19 at 07:47
  • Also keep in mind that your server has timeout for every query. Maybe better idea is to move file processing to cron. – marv255 Jun 27 '19 at 07:48
  • You can check the old one solution link is given below https://stackoverflow.com/questions/16524203/php-auto-kill-a-script-if-the-http-request-is-cancelled-closed – Anil Jat Jun 27 '19 at 07:56
  • If you don't mind using a package, you can use [Laravel Excel](https://github.com/maatwebsite/Laravel-Excel). It works very well importing/exporting large csv files. – CDO Jun 27 '19 at 08:01

0 Answers0