問題描述
我一開始就遇到了 PhP 文件上傳進度監視器的問題.
I have a problem with PhP File Upload progress monitor at the very start.
首先,這里是相關的 PhP.ini 設置(指令、本地值和主值):
First, here are the relevant PhP.ini settings (Directive, Local Value and Master Value):
session.upload_progress.cleanup On On
session.upload_progress.enabled On On
session.upload_progress.freq 1% 1%
session.upload_progress.min_freq 1 1
session.upload_progress.name PHP_SESSION_UPLOAD_PROGRESS PHP_SESSION_UPLOAD_PROGRESS
session.upload_progress.prefix upload_progress_ upload_progress_
這是表格(簡化版):
<form id="fileupload" style="position:relative;" target="iframe_fileupload" action="http://www.athiyoga.org/testupload.php" method="POST" enctype="multipart/form-data">
<input type="hidden" name="<?echo ini_get("session.upload_progress.name");?>" value="first"/>
<input type="file" name="file_1">
<button type="submit" >Start Submit</button>
</form>
我有 JQUERY Ajax 代碼,在同一個 PhP 文件中(當然,作為一個 JS 腳本),如:
I have JQUERY Ajax code, in the same PhP file (of course, as a JS script), as in:
$('#fileupload').submit(function(event){
//UPDATED THIS PART after reading: http://stackoverflow.com/questions/19336610/delay-in-populating-session-upload-progress-data
//POSTING the magic variable PHP_SESSION_UPLOAD_PROGRESS during status inquiry too
var params = {PHP_SESSION_UPLOAD_PROGRESS:"first", some_var:20 };
var data_params = jQuery.param( params );
setTimeout(function(){
upload_promise = $.ajax({
url: 'upload_status.php',
data: data_params,
dataType: 'html',
type : 'POST',
cache : false
});
$.when(upload_promise).done(function(status_response){
$('#response_status').html(status_response);
});
},5000);
...
...
upload_status.php 只是回應 $_SESSION 數組.我還在 form-php 中設置了一個測試會話變量,以確保 AJAX(通過 upload_status.php)選擇該會話變量.確實如此.但不是 $_SESSION 數組中上傳狀態的標志(無變量/索引)!文件被上傳.我確保文件足夠大,以便 5000 毫秒足以報告一些中間狀態.
The upload_status.php simply echoes the $_SESSION array. I also set a test session variable in the form-php to make sure that the AJAX (thru upload_status.php) picks that session variable. It does. But not a sign (no variable/index) of the upload status in the $_SESSION array! The files get uploaded. I made sure that the files are big enough so that the 5000ms is sufficient to report some intermediate status.
我之前從未實現過 PhP 文件上傳進度條,所以我想知道我是否遺漏了什么.一旦我在上傳中獲得一個狀態點,我就可以完成其余的工作.
I have never implemented PhP file upload progress bar before so I wonder if I am missing something. Once I get one status-point in the upload, I will be able to do the rest.
謝謝
推薦答案
可能存在一些問題,我已經列出了其中的幾個.
There could be some issues, I have listed down few of them.
- 必須禁用 Web 服務器的請求緩沖才能使其正常工作,否則 PHP 可能僅在完全上傳后才能看到文件上傳.
- 當您的網絡服務器通過 FastCGI 運行 PHP 時,此功能不起作用.
- 不要忘記,必須在生成表單之前初始化會話,否則您將無法在會話中獲得任何信息.
- 它不適用于 PHP 5.3 或更早版本.
- 請注意,如果您運行該代碼并打印出 $_SESSSION[$key] 的內容,您會得到一個空數組,因為 session.upload_progress.cleanup 默認是打開的,并且它會在所有 POST 后立即清除進度信息數據已讀取.將其設置為 Off 或 0 以查看 $_SESSION[$key] 的內容.
這可以幫助您跟蹤進度條http://pecl.php.net/package/uploadprogress
This can help you to track your progress bar http://pecl.php.net/package/uploadprogress
我希望這能幫助您找出問題所在.
I hope this will help you to dig out the problem.
這篇關于PhP 5.4 中的 PhP 上傳進度不起作用.會話變量未設置的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!