0

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

不幸的是,该脚本无法正常工作。我已确保在 php.inisession.upload_progress = On中启用了会话。我已经通过使用 4 中的两个简单 php 文件验证了会话通常可以正常工作。在此处发布http://quirm.net/forum/topic.php?id=3950

该脚本应该显示加载屏幕和文件上传的百分比。它是通过progress.php每 0.2 秒发送一次请求来实现的。不幸的是,无论文件有多大(我使用 ~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;
}
4

1 回答 1

1

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

于 2014-07-10T14:01:09.387 回答