Simple AJAX file reader!

Okay, they're not good at describing these things, so bear with me ..

I'm trying to find a way to display a status / progress bar to show the percentage (or whatever) of which part of a text file has been read / parsed by php. I know roughly what I need to do (be it a line count, or file size, and an actual text file), but I can't let my life determine how to implement it. I am using PHP / AJAX and all I really want is for one div to update every time (5 seconds or whatever) to show the status until everything is complete and then show the output. Easy, but how the hell am I supposed to do this, I don't know. I found some really helpful posts here, but nothing that I can quite work with what I have.

If anyone can give me some rough advice / ideas I would be stupidly grateful (it really does my head)! Thanks in advance!

+2


source to share


2 answers


What about...

We have to assume that there is a way to determine the total job (100%) and the point the php script is at (status% done), so if it reads / parses a text file, you can run the parse function by writing the total number of lines to db or text file. Then it can also write what line number it is to the same file every 5 seconds. The js ajax function calls this text file to get the total and the point at which it is included. When the php text parser is finished, it destroys the state file so that it doesn't take up space on the server, file name conflicts, etc.


Example:

First the (jquery) ajax POST function to server:



 $.post("parser.php", { file: "somefile.txt"} );
 //I admit, I'm not sure if this is how a file would be posted with ajax

      

Then php pulls the file and runs the parser function:

    $tmpName  = $_FILES['userfile']['tmp_name'];
    $fileName  = $_FILES['userfile']['name'];

    //Turn the file into an array, so that you can use the array count for status:
    $content = file($tmpName);

    // Get the array count as the total, as it equals the line count:
    $total = count($content);

    //Write the filesize to a unique "total" txt file:
    $total_file = fopen($fileName."-total.txt", 'x');
    fwrite($total_file, $total);
    fclose($total_file);

    //Pass the Array to the parser function:
    parser($content, $fileName);

    //Kill the file when the function is done:
    unlink($fileName);

    function parser ($file_array, $filename) {
           //creates the status file and then closes it, to ensure that it
           //exists for the loop but gets overwritten each time

           $status_file = fopen($filename."-status.txt", 'x');
           fclose($status_file);

           foreach($file_array as $position => $data) {
                  //Do your parsing here //
                   .............
                  //reopen status file and wipe out what is in it already:
                  $status_file = fopen($filename."-status.txt", 'w');
                  fwrite($status_file, $position);
                  fclose($status_file);
             }

       }

      

Since the status and the shared file share the hopefully unique name of the uploaded file, the ajax function knows where to look. He can do this:

 total = $.get("somefile-total.txt");

 current = $.get("somefile-status.txt");

 status = current/total;

      

+1


source


JQuery has some really good libraries that do exactly what you ask. If it's not just for educational purposes, I would look at them and not rebuild the wheel.



http://plugins.jquery.com/search/node/upload

0


source







All Articles