3

I am trying to create an upload progress bar with PHP. I saw the new feature of PHP 5.4: upload progress session.

This is my HTML code:

<form id="upload" action="ajax/progress.php" method="POST" enctype="multipart/form-data">
    <input type="hidden" name="<?php echo ini_get("session.upload_progress.name"); ?>" value="dupload" />
    <input id="file1" type="file" name="file1" />
    <input class="btn primary" type="submit" value="Upload" />
</form>

And this is progress.php:

<?php
session_start();

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

if (!empty($_SESSION[$key])){
    $current = $_SESSION[$key]["bytes_processed"];
    $total = $_SESSION[$key]["content_length"];
    echo $current < $total ? ceil($current / $total * 100) : 100;
}
else {
    var_dump($_SESSION);
    var_dump($_FILES);
}

AJAX:

$('#upload').submit(function () {
    interval_id = setInterval(function () {
        $.ajax({
            url: "ajax/progress.php6",
            type: "POST",
            success: function (data) {
                console.log(data);
            }
        });
    }, 200);
    return false;
});

All the ini settings are right. (session is enabled, name and prefix is right)

I am always getting an empty session array. What's wrong?

Thanks!

7
  • 1
    Your reading not writing to the session... how woukd it ever not be empty Commented Jul 13, 2012 at 19:17
  • What do you mean? There's nothing in the doc saying I should manually write to the session, it should write it automatically. Commented Jul 13, 2012 at 20:52
  • If your file really named "progress.php6"? Commented Jul 13, 2012 at 21:02
  • Yup, php6 extension is required in my server if I want to use PHP 5.4.. Commented Jul 13, 2012 at 21:04
  • Are you running PHP via FastCGI? php.net/manual/en/session.upload-progress.php#109091 Commented Jul 13, 2012 at 21:04

3 Answers 3

1

Wasn't able to sort it out so I used XMLHTTPREQUEST "progress" action and that worked well. Thanks!

Sign up to request clarification or add additional context in comments.

1 Comment

I think, that this one SHOULD not be accepted ;). Maybe it is a solution, but ABSOLUTELY it isn't an answer...
0

It's not possible in one POST request, you can do AJAX requests, and read session while file is uploading. After file is uploaded, the session.upload_progress.name key is removed so you are getting an empty array.

2 Comments

Well I also tried settings an AJAX request every second after a form submit, empty array as well.
What server u use ? if nginx there are some problems with this new feature of php, and i have to use iframe+apache to handle progress bars : )
0

I think it's because your form is not getting submitted, as you have return false in

$('#upload').submit(function () {
    interval_id = setInterval(function () {
        $.ajax({
            url: "ajax/progress.php6",
            type: "POST",
            success: function (data) {
                console.log(data);
            }
        });
    }, 200);
    return false;
});

and it initializes interval, so the page always return null as nothing is being uploaded. Try using http://www.malsup.com/jquery/form, it submits form

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.