Use $_FILES on a page called by .ajax

Posted by RachelD on Stack Overflow See other posts from Stack Overflow or by RachelD
Published on 2012-12-13T23:02:28Z Indexed on 2012/12/13 23:03 UTC
Read the original article Hit count: 377

Filed under:
|
|
|

I have two .php pages that I'm working with. Index.php has a file upload form that posts back to index.php. I can access the $_FILES no problem on index.php after submitting the form.

My issue is that I want (after the form submit and the page loads) to use .ajax (jQuery) to call another .php file so that file can open and process some of the rows and return the results to ajax. The ajax then displays the results and recursively calls itself to process the next batch of rows.

Basically I want to process (put in the DB etc) the csv in chunks and display it for the user in between chunks. Im doing it this way because the files are 400,000+ rows and the user doesnt want to wait the 10+ min for them all to be processed.

I dont want to move this file (save it) because I just need to process it and throw it away and if a user closes the page while its processing the file wont be thrown away. I could cron script it but I dont want to.

What I would really like to do is pass the (single) $_FILES through .ajax OR Save it in a $_POST or $_SESSION to use on the second page.

Is there any hope for my cause?

Heres the ajax code if that helps:

    function processCSV(startIndex, length)
    {
        $.ajax({ 
            url: "ajax-targets/process-csv.php", 
            dataType: "json",
            type: "POST",
            data: { startIndex: startIndex, length: length },
            timeout: 60000, // 1000 = 1 sec
            success: function(data) {
                // JQuery to display the rows from the CSV

                var newStart = startIndex+length;
                if(newStart <= data['csvNumRows']) {
                    processCSV(newStart, length);
                }
            }
        });
    }
    processCSV(1, 2);
});

P.S. I did try this Passing $_FILES or $_POST to a new page with PHP but its not working for me :( SOS.

© Stack Overflow or respective owner

Related posts about php

Related posts about AJAX