为什么apc_fetch没有返回上传进度?

时间:2012-09-06 06:31:54

标签: php file-upload xmlhttprequest apc

我正在尝试获取文件上传的进度,但apc_fetch不会为此返回数组:

$status = apc_fetch('upload_'.$_REQUEST['progress_key']);

文件上传成功完成且没有错误。我启用了apc.rfc1867。我知道APC_UPLOAD_PROGRESS必须是发送到服务器的第一个参数。我正在使用XMLHttpRequest上传,并且APC_UPLOAD_PROGRESS正在查询字符串中发送到服务器。

上传发生在我服务器上的虚拟主机上。我在/etc/php.d/apc.ini

中启用了APC

以下是我用于上传的Javascript:

xhr.open('POST', queryURL, true); // queryURL var contains APC_UPLOAD_PROGRESS
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
xhr.setRequestHeader('X-File-Name', encodeURIComponent(filename));
xhr.setRequestHeader('Content-Type', 'application/octet-stream');               
xhr.send(input.files[0]); 

当我使用iframe进行上传时,它在IE9中也不起作用。

为什么apc_fetch不返回上传进度数组?

2 个答案:

答案 0 :(得分:2)

您的APC_UPLOAD_PROGRESS隐藏字段是否位于表单中的文件输入标记之前?

答案 1 :(得分:1)

演示网址: -

http://jquery.malsup.com/form/progress.html

您可以从此网址下载jQuery文件并添加html标记

http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js

http://malsup.github.com/jquery.form.js

试试这个: -

//这是我的html标记

<!doctype html>
<head>
<title>File Upload Progress Demo #1</title>
<style>
body { padding: 30px }
form { display: block; margin: 20px auto; background: #eee; border-radius: 10px; padding: 15px }

.progress { position:relative; width:400px; border: 1px solid #ddd; padding: 1px; border-radius: 3px; }
.bar { background-color: #B4F5B4; width:0%; height:20px; border-radius: 3px; }
.percent { position:absolute; display:inline-block; top:3px; left:48%; }
</style>
</head>
<body>
    <h1>File Upload Progress Demo #1</h1>
    <code>&lt;input type="file" name="myfile"></code>
        <form action="upload.php" method="post" enctype="multipart/form-data">
        <input type="file" name="uploadedfile"><br>
        <input type="submit" value="Upload File to Server">
    </form>

    <div class="progress">
        <div class="bar"></div >
        <div class="percent">0%</div >
    </div>

    <div id="status"></div>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script>
<script src="http://malsup.github.com/jquery.form.js"></script>
<script>
(function() {

var bar = $('.bar');
var percent = $('.percent');
var status = $('#status');

$('form').ajaxForm({
    beforeSend: function() {
        status.empty();
        var percentVal = '0%';
        bar.width(percentVal)
        percent.html(percentVal);
    },
    uploadProgress: function(event, position, total, percentComplete) {
        var percentVal = percentComplete + '%';
        bar.width(percentVal)
        percent.html(percentVal);
    },
    complete: function(xhr) {
     bar.width("100%");
    percent.html("100%");
        status.html(xhr.responseText);
    }
}); 

})();       
</script>

</body>
</html>

我的PHP代码:

<?php
$target_path = "uploads/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";
}
?>