Reputation: 7942
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;
})();
Upvotes: 0
Views: 1614
Reputation: 180
As gavin says if the php handler is fast cgi wont work
I changed the php handler in whm admin panel to suphp and worked like a charm, however phpinfo() still shows fastcgi, so phpinfo its not reliable to check this information.
Upvotes: 1
Reputation: 7942
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".
Upvotes: 0