Ajax progress with PHP session

Posted by FFish on Stack Overflow See other posts from Stack Overflow or by FFish
Published on 2010-05-20T19:00:58Z Indexed on 2010/05/20 19:10 UTC
Read the original article Hit count: 209

Filed under:
|
|
|

I have an app that processes images and use jQuery to display progress to the user.
I done this with writing to a textfile each time and image is processed and than read this status with a setInterval.

Because no images are actually written in the processing (I do it in PHP's memory) I thought a log.txt would be a solution, but I am not sure about all the fopen and fread's. Is this prone to issues?

I tried also with PHP sessions, but can't seem to get it to work, I don't get why..

HTML:

<a class="download" href="#">request download</a>
<p class="message"></p>

JS:

$('a.download').click(function() {

    var queryData = {images : ["001.jpg", "002.jpg", "003.jpg"]};       
    $("p.message").html("initializing...");

    var progressCheck = function() {
        $.get("dynamic-session-progress.php",
            function(data) { 
                $("p.message").html(data); 
            }
        );
    };

    $.post('dynamic-session-process.php', queryData,
        function(intvalId) {
            return function(data) {
                $("p.message").html(data);
                clearInterval(intvalId);
            }
        } (setInterval(progressCheck, 1000))
    );

    return false;
});

process.php:

// session_start();

$arr = $_POST['images'];
$arr_cnt = count($arr);
$filename = "log.txt";

for ($i = 1; $i <= $arr_cnt; $i++) {
    $content = "processing $val ($i/$arr_cnt)";

    $handle = fopen($filename, 'w');
    fwrite($handle, $content);
    fclose($handle);

    // $_SESSION['counter'] = $content;

    sleep(3); // to mimic image processing
}

echo "<a href='#'>download zip</a>";

progress.php:

// session_start();

$filename = "log.txt";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);

echo $contents;

// echo $_SESSION['counter'];

© Stack Overflow or respective owner

Related posts about php

Related posts about AJAX