【问题标题】:Session Upload Progress, Ajax call and empty $_SESSION会话上传进度、Ajax 调用和空 $_SESSION
【发布时间】:2013-06-17 07:23:15
【问题描述】:

我的目标是,在单击表单提交按钮后,将附件从表单上传到服务器并显示进度条,然后提交表单(即邮寄消息)。

上传表单.php:

<form action="email_message.php" method="post" enctype="multipart/form-data" name="fileform" id="fileform">
    <input type="hidden" name="MAX_FILE_SIZE" value="50000000"/>
    <input type="hidden" name="<?php echo ini_get("session.upload_progress.name"); ?>" value="uploads"/>
    <label for="userfile1">Upload a file:</label>
    <input type="file" name="userfile1" id="userfile1" class="userfile"/>   
    <input id="submit_btn" type="submit" value="Send Message"/>
</form>

在同一页面中,我运行以下代码以防止执行表单并发送从表单上传所有文件的请求。

$(document).ready(function(){
    $("#fileform").submit(function(e){
        e.preventDefault();
        var self = this;

        var formData = new FormData(document.getElementById("fileform"));
        var upload_req = $.ajax({
            url: "./upload_multiple_files.php",
            type: "POST",
            data: formData,
            processData: false,
            contentType: false
        });
        upload_req.done(function(data){
            alert("Uploading complete!");
        });
    });
});

upload_multiple_files.php:

<?php
session_start();

foreach ($_FILES as $k => $v){
    // code to deal with file errors

    if (is_uploaded_file($v['tmp_name'])){
        // code to rename file

        echo "<p>The file was successfully uploaded.</p>";
    }else{
        echo "<p>The file was not uploaded.</p>";
    }
}
?>

所有这些都有效:文件全部上传到服务器。

我遇到的问题是集成 PHP Upload Session Progress (http://php.net/manual/en/session.upload-progress.php)。

我知道我需要使用session.upload_progress.name$_POST 数组来获取文件上传信息,但我不确定将它放在哪里。我想创建一个带有间隔的 ajax 调用,以定期获取要在我的表单页面上显示的上传进度。但是,当我创建一个新页面时,会话信息为空。这是我尝试过的页面示例:

get_progress.php:

<?php
session_start();

// $key is a combination of session.upload_progress.prefix and session.upload_progress.name

$results = array("content_length" => $_SESSION[$key]['content_length'],
    "bytes_processed" => $_SESSION[$key]['bytes_processed']
);

echo json_encode($results);
?>

我检查了来自upload_form.phpget_progress.php 的会话ID,它们是相同的。

$_SESSIONget_progress.php 中为空的任何原因?我想我错过了一些简单的事情,但我无法弄清楚。

【问题讨论】:

  • 你有没有试过print_r();看看$_SESSION里面是不是真的什么都没有
  • 是的。它只是返回 Array()。
  • 尝试在上传任何照片之前设置会话。检查您是否看到该会话
  • upload_multiple_files.php 中,在session_start(); 之后,$_SESSION 数组为空。
  • 检查您的 php ini 设置或 session.upload_progress.enabled 已启用。请检查phpinfo();

标签: php


【解决方案1】:

@Perry 你的回答是对的,session.upload_progress.enabled 没有在 php.ini 中启用。谢谢。

【讨论】:

    猜你喜欢
    • 2014-12-10
    • 2013-01-04
    • 2013-10-27
    • 1970-01-01
    • 2015-05-30
    • 1970-01-01
    • 2012-06-13
    • 1970-01-01
    • 2011-06-18
    相关资源
    最近更新 更多