Wednesday 15 May 2013

javascript - Upload Progress - Sometimes $_SESSION[$key] is Empty -



javascript - Upload Progress - Sometimes $_SESSION[$key] is Empty -

i have ubuntu 12.04 lts , using php 5.5 apache2 implement upload progress via php session upload progress.

the issue works , doesn't work. mean progress percentage 100% direct @ origin of upload without finishing upload (which means $_session[$key] empty in cases, why?!)

i tried turning value of session.upload_progress.cleanup on , off, didn't alter anything.

you can seek on url: http://138.128.124.172/upload_progress

in php.ini, have below settings related upload:

;;;;;;;;;;;;;;;; ; file uploads ; ;;;;;;;;;;;;;;;; ; whether allow http file uploads. ; http://php.net/file-uploads file_uploads = on ; temporary directory http uploaded files (will utilize scheme default if not ; specified). ; http://php.net/upload-tmp-dir ;upload_tmp_dir = ; maximum allowed size uploaded files. ; http://php.net/upload-max-filesize upload_max_filesize = 100m ; maximum number of files can uploaded via single request max_file_uploads = 20 ; enable upload progress tracking in $_session ; default value: on ; development value: on ; production value: on ; http://php.net/session.upload-progress.enabled session.upload_progress.enabled = on ; cleanup progress info post info has been read ; (i.e. upload completed). ; default value: on ; development value: on ; production value: on ; http://php.net/session.upload-progress.cleanup session.upload_progress.cleanup = off ; prefix used upload progress key in $_session ; default value: "upload_progress_" ; development value: "upload_progress_" ; production value: "upload_progress_" ; http://php.net/session.upload-progress.prefix ;session.upload_progress.prefix = "upload_progress_" ; index name (concatenated prefix) in $_session ; containing upload progress info ; default value: "php_session_upload_progress" ; development value: "php_session_upload_progress" ; production value: "php_session_upload_progress" ; http://php.net/session.upload-progress.name ;session.upload_progress.name = "php_session_upload_progress" ; how upload progress should updated. ; given either in percentages (per-file), or in bytes ; default value: "1%" ; development value: "1%" ; production value: "1%" ; http://php.net/session.upload-progress.freq ;session.upload_progress.freq = "1%" ; minimum delay between updates, in seconds ; default value: 1 ; development value: 1 ; production value: 1 ; http://php.net/session.upload-progress.min-freq ;session.upload_progress.min_freq = "1"

at php side: have below code within page: progress.php:

session_start(); $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; }

at client side, have below code in page index.php

<?php if ($_server["request_method"] == "post" && !empty($_files["userfile"])) { // move_uploaded_file() } ?> <style> #bar_blank { border: solid 1px #000; height: 20px; width: 300px; } #bar_color { background-color: #006666; height: 20px; width: 0px; } #bar_blank, #hidden_iframe { display: none; } </style> <html> <head> <title>file upload progress bar</title> </head> <body> <div id="bar_blank"> <div id="bar_color"></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"></iframe> </body> </html> <script> 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 net explorer") { http = new activexobject("microsoft.xmlhttp"); } else { http = new xmlhttprequest(); } homecoming 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; //alert(response);return; 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."; document.getelementbyid("bar_color").style.width = 0 + "%"; } } } function startupload() { togglebarvisibility(); settimeout("sendrequest()", 1000); } (function () { document.getelementbyid("myform").onsubmit = startupload; })(); </script>

i not interested in html5, jquery or flash. thankful if hint me improve approaches robust way implement upload progress bar.

thanks help!

i utilize reply part due size of answer. or, size of details... in fact i've have same problem, php 5.5.18 running on debian whezzy.

after making few test , putting log in progress.php in order save value of $key, bytes_processed , content_length, here conclusions:

discovery 1: don't have empty key. have key showing informations bytes_processed = content_length

discovery 2: if download eg 4 files different size , have @ log of progress.php you'll see value session sec file give result file 1.

example:

send test.docx -> 500.000 bytes. $key empty

send house.jpg -> 4.000.000 bytes. $key give bytes_processed = content_length = 500.000 result of previous file

in many case, utilize in form, hidden field this:

echo "<input type=hidden value=\"myform\" name=\""; echo ini_get("session.upload_progress.name"); echo "\" />\n";

and info using in progress.php:

$key = ini_get("session.upload_progress.prefix") . "myform";

meaning all our $key have same name. alter by:

$val = time(); echo "<input type=hidden value=\"".$val."\" name=\""; echo ini_get("session.upload_progress.name"); echo "\" />\n";

and

$key = ini_get("session.upload_progress.prefix") . $_post[ini_get("session.upload_progress.name")];

now, each time have empty key. conclusion have cache problem php.net say:

warning web server's request buffering has disabled work properly, else php may see file upload 1 time uploaded. servers such nginx known buffer larger requests.

javascript php session file-upload progress-bar

No comments:

Post a Comment