Note that there are some explanatory texts on larger screens.

plurals
  1. POAjax progress with PHP session
    primarykey
    data
    text
    <p>I have an app that processes images and use jQuery to display progress to the user.<br /> I done this with writing to a textfile each time and image is processed and than read this status with a setInterval.</p> <p>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?</p> <p>I tried also with PHP sessions, but can't seem to get it to work, I don't get why..</p> <p>HTML:</p> <pre><code>&lt;a class="download" href="#"&gt;request download&lt;/a&gt; &lt;p class="message"&gt;&lt;/p&gt; </code></pre> <p>JS:</p> <pre><code>$('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; }); </code></pre> <p>process.php:</p> <pre><code>// session_start(); $arr = $_POST['images']; $arr_cnt = count($arr); $filename = "log.txt"; for ($i = 1; $i &lt;= $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 "&lt;a href='#'&gt;download zip&lt;/a&gt;"; </code></pre> <p>progress.php:</p> <pre><code>// session_start(); $filename = "log.txt"; $handle = fopen($filename, "r"); $contents = fread($handle, filesize($filename)); fclose($handle); echo $contents; // echo $_SESSION['counter']; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload