PHP setCookie inside Curl Progress Function

Currently I have a progress function Curl is using to write the current progress to a txt file. From there I have Jquery doing an ajax GET to get the current progress of the download. This all is working properly but I want to move from using a file to using a cookie and then have jquery read that cookie instead of working with actual files. Right now I modified the progress callback function to use setcookie but I am seeing an issue that is breaking things on the jquery end of things. I see where the cookie is being set but it doesn't get updated again until the progress is 100. So basically the progress cookie jumps from 0 to 100 percent and is not representative of the actual progress. Here is that function as it is now (you can see in the commented code where I was writing to a file:

function progressCallback($resource, $download_size, $downloaded_size, $upload_size, $uploaded_size)
{
    global $workingDir;
    static $previousProgress = 0;

    if ($download_size == 0) {
        $progress = 0;
    } else {
        $progress = round($downloaded_size * 100 / $download_size);
        if ($progress > $previousProgress) {
            $previousProgress = $progress;
            //$fp = fopen($workingDir.'progress.txt', 'w');
            //fputs($fp, "$progress");
            //fclose($fp);
            setcookie('progressPercent', $progress, 0, '/', '', 0, 0);
        }
    }
}

Open in new window

ITNCAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Slick812Commented:
greetings ITNC, , you are using php to "SET" a cookie here, , however, your problem may be that the browser, is not periodically "READING" this cookie and updating the the percentage display. you used to use a periodic "Ajax" file retrieve to update the percentage, , in what kind of repeating process in the javascript of the browser, are you reading the cookie "progressPercent" to update the display?
ITNCAuthor Commented:
I am using the jquery cookie plugin inside a function that is called using setInterval

percentage = $.cookie("progressPercent");

Open in new window


This is how I am reading the cookie currently each time the function executes.
Slick812Commented:
I looked at the  jquery cookie plugin web page that you gave a link to, and it seems that  $.cookie("progressPercent")  would read the cookie, , have you verified that the   $.cookie("progressPercent")   does get a value from your cookie? ?

I can not see any missed functional code in the code blocks you show, sorry.

I have seen code examples  that do an AJAX file upload, and show a progressing percentage of completion on a display in the browser. However, I believe that there is more to this on the browser side than a cookie value. Many of the available Jquery File up-load plug-ins have the "progress bar" already built into them, but I do not have time now to try find the code work in these that do a progress bar, and offer you an alternative solution.

I looked at this page about jquery file progress bar -
      http://stackoverflow.com/questions/15410265/file-upload-progress-bar-with-jquery
and they make several sugestions for a solution.

there were hundreds of other pages about the jquery file progress bar in my web search?

The progress bar is not uncommon for this operation, perhaps the documentation for your version of a jquery file uploader has code examples to do this?
Slick812Commented:
I have looked into the problem with using a cookie, and a cookie will not work for the AJAX file upload. The reason is that the PHP page that is getting the file from browser, Does NOT return to ajax and deliver a cookie (just ONE cookie), Until the file and all php is finished uploading. Whereas , the file write you use -  fputs($fp, "$progress");  -  changes that file several times during the upload progression, without returning to the browser ajax, that's why the file write works and cookie does not.  You might stick with the file write method, I beleive it is one of the simplest ways to do a progress. You could find another way, but as far as I can see, maybe all ways would use an ajax to server to get the progress amount, You might could use a
      $_SESSION['progress'] = $progress;
and then call a php page  "progress.php" with ajax, and just echo the session prog as -
      echo $_SESSION['progress'];
and set that amount on your page display. This may can prevent a mixup of your current file-write  "file name", if several users are uploading at the same time.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ITNCAuthor Commented:
I was thinking the same thing unfortunately. I will consider the $_SESSION approach but I'm thinking I'll just stick with the file method for now. My research ended in the same conclusion so I feel verified now, I will mark that as the answer thanks for your time!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.