1

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.

To clarify: My problem is that I want to access a $_FILE on a page that is called by ajax. The file is uploaded on index.php, the form as action="#" so it posts to index.php. After the post index.php sends an ajax call to process-csv.php and I need the file to be accessible on process-csv.php. I do not want to move the file because I don't want to have to clean up old files.

Community
  • 1
  • 1
RachelD
  • 4,072
  • 9
  • 40
  • 68

2 Answers2

1

So I don't think you ever say your problem, but I'm guessing your problem is you are not able to ajax a file. The reason for this is because you can't actually get the file information in javascript because it's a security risk. There is a bunch of ways you can do this though. You can either use flash or an iframe to fake a ajax like file upload.

jQuery iframe file upload

I actually like the flash version though cause it gives you the ability to upload multiple files at once and still offers everything the iframe does as well as many more events.

http://code.google.com/p/swfupload/

With uploadComplete you can use to then put your processing code you have and uploadStart to use to pass a marker to say to link this session up with the file added to the database.

Also in your processing make sure you are always passing how far you have gotten, so it doesn't keep returning back the same rows each time.

I hope that helps.

Community
  • 1
  • 1
Ian Overton
  • 1,060
  • 7
  • 17
  • I was actually hoping to stick the $_FILE into a $_SESSION or something similar. I know that post/get will not work so I was looking for another option. I didn't go the jquery upload route because the file is temporary and after its processed I want it to be deleted from the server. JQuery seemed a little robust for that. I will look into the links though thank you. – RachelD Dec 14 '12 at 15:47
  • A little different then your $_SESSION idea is I actually coppied a unique file name to the /tmp/ folder (based off the page they are on and the session) and stored that in session, so I don't have to have files in session and worry about if I'm going to over write the stuff in session already. – Ian Overton Dec 14 '12 at 16:08
  • You should check the (relatively) new [addition to XMLHttpRequest](https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRequest/FormData/Using_FormData_Objects) :) – Ja͢ck Dec 15 '12 at 01:18
1

Just construct the data rows in JavaScript then:

data: { 
    startIndex: startIndex, 
    length: length, 
    rows: variableWithRows.slice(startIndex, startIndex + length)
},

Inside PHP, all rows would be in $_POST['rows'] as array data.

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
  • This may be a good option. I think I can modify my csv-processing object to handle this. Thank you. – RachelD Dec 14 '12 at 15:45