weixin_33693070 2010-05-20 19:00 采纳率: 0%
浏览 19

Ajax与PHP会话的进度

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'];
  • 写回答

1条回答 默认 最新

  • helloxielan 2010-05-20 19:07
    关注

    What if two clients process images at the same time?

    You can try adding session_write_close() between setting the new status in the session, so that the new session data is stored, otherwise it will only get stored once your script finishes.

    Another solution would be to save the status in memcache or to use a database, perhaps separate the statuses with a userid or creating an md5 hash on the image data

    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘