用PHP会议阿贾克斯进展 [英] Ajax progress with PHP session

查看:130
本文介绍了用PHP会议阿贾克斯进展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序处理图像,并使用jQuery来显示进度给用户。
我每一次写入文本文件这样做,图像处理和比读取该状态用的setInterval。

由于没有图像实际上写的是在处理(我这样做是在PHP的内存),我想到了一个log.txt的将是一个解决办法,但我不知道所有的fopen和FREAD的。这是容易出现问题?

我也试过用PHP会议,但似乎无法得到它的工作,我不知道为什么。

HTML:

 <一类=下载的href =#>请求下载< / A>
&其中,P类=消息>< / P>
 

记者:

  $('a.download)。点击(函数(){

    VAR queryData = {图片:001.jpg,002.jpg,003.jpg]};
    $(p.message)HTML(初始化...);

    VAR progressCheck =功能(){
        $获得(动态会话progress.php
            功能(数据){
                $(p.message)的HTML(数据);
            }
        );
    };

    .post的$('动态会话process.php,queryData,
        功能(intvalId){
            返回功能(数据){
                $(p.message)的HTML(数据);
                clearInterval(intvalId);
            }
        }(的setInterval(progressCheck,1000))
    );

    返回false;
});
 

process.php:

  //在session_start();

$ ARR = $ _ POST ['形象'];
$ arr_cnt =计数($ ARR);
$文件名=log.txt的;

为($ i = 1; $ I< = $ arr_cnt; $ I ++){
    $内容=处理$ VAL($ I / $ arr_cnt);

    $处理=的fopen($文件名,'W');
    FWRITE($处理,$内容);
    fclose函数($处理);

    // $ _SESSION ['反'] = $内容;

    睡眠(3); //模拟图像处理
}

回声&所述; A HREF =#>下载拉链&所述; / A>中;
 

progress.php:

  //在session_start();

$文件名=log.txt的;
$处理=的fopen($文件名,R);
$内容=的fread($处理,档案大小($文件名));
fclose函数($处理);

回声$内容;

//回声$ _SESSION ['反'];
 

解决方案

如果两个客户端在同一时间处理的图像?

您可以尝试添加设置在会话中新的地位,从而使新的会话数据存储之间的session_write_close(),否则只会被存储一次你的脚本完成。

另一个解决方案是在其中保存内存缓存或使用数据库的状态, 也许分开的状态的用户用户名或对图像数据创建MD5哈希

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'];

解决方案

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

这篇关于用PHP会议阿贾克斯进展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆