基于会话不能正常运行的PHP + Javascript上传

时间:2013-05-09 09:14:08

标签: php javascript session

我在buildroot嵌入式linux上使用运行FastCGI的lighttpd + PHP 5.4。 我前段时间使用以下教程编写了PHP文件上传进度脚本的修改版本:http://phpmaster.com/tracking-upload-progress-with-php-and-javascript/ 我以前使用mod_php在Slackware Apache上工作,一切正常。

不幸的是,脚本无效。我确保在php.ini session.upload_progress = On中启用了会话。我已经验证会话通常使用两个简单的php文件,如4. {post http://quirm.net/forum/topic.php?id=3950

该脚本应显示加载屏幕和文件上载百分比。它是通过每0.2秒发送progress.php个请求来实现的。不幸的是,无论文件有多大(我使用~13 MB文件进行测试,我确保php.ini中的文件上传大小接受此大小),上传在第一次请求后结束,因为它返回100.我已经重新调整了进度.php,它看起来整个$ _SESSION变量都是空的。

这是我的表单HTML代码:

<form enctype="multipart/form-data" action="actions/upload_upgrade.php" method="post" target="hidden_iframe" onsubmit="upload_type='config'; startUpload('config_form')" id="config_form">
                <p class="stdtext">
                Select File:
                <input type="hidden" name="type" value="config">
                <input type="hidden" value="config_form"
                name="<?php echo ini_get("session.upload_progress.name"); ?>">
                <input type="file" size="35" name="userfile" />
                <br><br>
                <input type="submit" class="defaultbtn" name="Upload" value="Upload" onclick="showLoadingScreen(true)"/>
                </p>
            </form>

我的Javascript代码(请注意,上传和响应处理功能是通用的,因为它们接受来自用于上传配置,固件等的几种不同表单的上传。)

function sendUploadRequest(form_name)
{
    job_name="Upload";
    var http = createRequestObject();
    http.open("GET", "actions/progress.php?form_name="+form_name);
    http.onreadystatechange = function () { handleResponse(http,"sendUploadRequest('"+form_name+"')"); };
    http.send(null);
}

function startUpload(form_name)
{
    showLoadingScreen(true);
    postDataSync("actions/uw.php");
    document.getElementById("loadingtext").innerHTML="Uploading file...";
    setTimeout("sendUploadRequest('"+form_name+"')", 200);
}

function handleResponse(http,fun_name)
{
    hideInfo();
    var response;
    if (http.readyState == 4) 
    {
        response = http.responseText;
        document.getElementById("loadingtext").innerHTML = response + "%";

        if (response < 100)
        {
            setTimeout(fun_name, 200);
        }
        else
        {
            showLoadingScreen(false);
            if(job_name=="")
                printInfo("Job completed successfuly");
            else
            {
                if(job_name=="Upload"&&upload_type=="config")
                {
                    if(hidden_iframe.window.document.body.innerHTML=="SUCCESS")
                    {
                        printInfo(job_name+" completed successfuly");
                        showLoadingScreen(true);
                        document.getElementById("loadingtext").innerHTML="Restoring backup...";
                        var result=postDataSync("actions/extract_backup.php");
                        showLoadingScreen(false);
                        if(result.substring(0,5)=="ERROR")
                        {
                            printError("Error while extracting backup configuration:<br><br>"+result.substring(6));
                        }
                        else if(result.substring(0,7)=="SUCCESS")
                        {
                            printInfo("Backup configuration restored successfully");
                        }
                        else
                        {
                            printError("Unknown error while extracting backup. Please contact service.");
                        }
                    }
                    else
                    {
                        printInfo(job_name+" was not completed because of errors");
                    }
                }
                else
                {
                    if(hidden_iframe.window.document.body.innerHTML=="SUCCESS")
                        printInfo(job_name+" completed successfuly");
                    else
                        printError(job_name+" was not completed because of errors");
                }
            }
        }
    }
}

function createRequestObject()
{
    var http;
    if (navigator.appName == "Microsoft Internet Explorer")
    {
        http = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else
    {
        http = new XMLHttpRequest();
    }
    return http;
}

我的PHP代码 progress.php

<?php
session_start();
//print_r($_SESSION);
$key = ini_get("session.upload_progress.prefix") . $_GET['form_name'];
if (!empty($_SESSION[$key])) {
    $current = $_SESSION[$key]["bytes_processed"];
    $total = $_SESSION[$key]["content_length"];
    echo $current < $total ? ceil($current / $total * 100) : 100;
}
else {
    echo 100;
}

1 个答案:

答案 0 :(得分:1)

http://de3.php.net/manual/de/session.upload-progress.php上说:“注意,当您的网络服务器通过FastCGI运行PHP时,此功能不起作用。”