doumouyi4039 2014-02-09 17:47
浏览 40

简单的PHP 5.4会话上传进度数据不在会话变量中

I've used the session upload progress method before and it has worked fine, but this is a new server and I'm having trouble. Everything appears correct. "session.upload_progress.enabled" is set to "On". My PHP version is 5.4.24. The 'session.upload_progress' name and prefix are both set to default values.

PHP sessions are working fine. I changed the session save path to an easily accessible folder and after calling session_start() if I do $_SESSION['test'] = 'test' it is written to the session file, but after starting the file upload no file info is being written to the file.

I told my host what is going on and they are holding firm on it being a problem with my code.

To make things simpler I'm using a very simple script to isolate the file uploading functionality.

EDIT: Sorry, I should say that what happens is in progress.php if (!empty($_SESSION[$key])) { is true each time (meaning $_SESSION[$key] is empty).

index.php

if ($_SERVER["REQUEST_METHOD"] == "POST" && !empty($_FILES["userfile"])) {
    // move_uploaded_file()
}
?>
<html>
    <head>
        <title>File Upload Progress Bar</title>
    </head>
    <body>
        <div id="bar_blank" style="display: none; border: solid 1px #000; height: 20px; width: 300px;">
            <div id="bar_color" style="background-color: #006666; height: 20px; width: 0px;"></div>
        </div>
        <div id="status"></div>
        <form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="POST" id="myForm" enctype="multipart/form-data" target="hidden_iframe">
            <input type="hidden" value="myForm" name="<?php echo ini_get("session.upload_progress.name"); ?>">
            <input type="file" name="userfile"><br>
            <input type="submit" value="Start Upload">
        </form>
        <iframe id="hidden_iframe" name="hidden_iframe" src="about:blank" style="display: none;"></iframe>
        <script type="text/javascript" src="script.js"></script>
    </body>
</html>

progress.php

$key = ini_get("session.upload_progress.prefix") . "myForm";
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;
}

script.js

function toggleBarVisibility() {
    var e = document.getElementById("bar_blank");
    e.style.display = (e.style.display == "block") ? "none" : "block";
}

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

function sendRequest() {
    var http = createRequestObject();
    http.open("GET", "progress.php");
    http.onreadystatechange = function () { handleResponse(http); };
    http.send(null);
}

function handleResponse(http) {
    var response;
    if (http.readyState == 4) {
        response = http.responseText;
        document.getElementById("bar_color").style.width = response + "%";
        document.getElementById("status").innerHTML = response + "%";

        if (response < 100) {
            setTimeout("sendRequest()", 1000);
        }
        else {
            toggleBarVisibility();
            document.getElementById("status").innerHTML = "Done.";
        }
    }
}

function startUpload() {
    toggleBarVisibility();
    setTimeout("sendRequest()", 1000);
}

(function () {
    document.getElementById("myForm").onsubmit = startUpload;
})();
  • 写回答

2条回答 默认 最新

  • dpw30157 2014-02-10 07:33
    关注

    It looks like I've tracked down the problem to the web server running PHP as FastCGI. FastCGI doesn't support session.upload_progress. You can check if your server is running PHP as FastCGI by creating a PHP file with phpinfo();. Under "Server API" mine says "CGI/FastCGI".

    评论

报告相同问题?

悬赏问题

  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)