Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Upload Progress Session

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!

like image 956
AdamGold Avatar asked Jul 12 '26 15:07

AdamGold


1 Answers

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

like image 71
AdamGold Avatar answered Jul 14 '26 05:07

AdamGold



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!