Hello.

I'm using a php script to upload file. I've used an iframe inserted on the upload page that refreshes every second/3sec. to display percentage uploaded.

forget percentage, lets take a simple example.

Presently, my iframe displays last two digits of PHP time() function that change constantly. If you see the live example at: http://mintload.com/upload/upload-single.php
The last two digits of time() function are shown and iframe is refreshed every 3sec.
But, the moment one clicks "Upload" button, the file starts uploading, but iframe no more refreshes & freezes. In IE7 it works fine. Why not in Firefox Latest Ver: 3.6 ?

code for iframe:

header("refresh:1; $_SERVER[PHP_SELF]");
echo substr(time(),-2,2);

My point is, just as in IE, my iframe shouldnt freeze refreshing even if a file is being uploaded. Any code/functions?

Please help. Thank you.

Dani AI

Generated

Short expert note (for and the thread):

This is not a PHP bug — it’s a networking/browser behavior. When a form POST uploads a file the browser may hold/queue other HTTP requests for that origin; Firefox’s Network Monitor labels that time as “Blocked” (waiting for a connection slot). That explains why your iframe’s periodic refresh can appear to “freeze” while the upload runs; different browsers handle connection/queueing differently (so IE7 can behave differently). (firefox-source-docs.mozilla.org)

A more reliable approach than meta-refreshing or reloading an iframe is to do the upload with XMLHttpRequest + FormData and read progress events on the client. That gives a real percent (no polling) and won’t be subject to the same iframe-queueing issue. Example (modern browsers):

const f = document.querySelector('#file');
const p = document.querySelector('#progress');

function send() {
  const fd = new FormData();
  fd.append('file', f.files[0]);

  const xhr = new XMLHttpRequest();
  xhr.open('POST', 'upload.php', true);

  xhr.upload.onprogress = (e) => {
    if (e.lengthComputable) p.value = (e.loaded / e.total) * 100;
  };

  xhr.onload = () => { /* handle response */ };
  xhr.send(fd);
}

Use this when you can — MDN documents the upload progress API and examples. (developer.mozilla.org)

If XHR isn’t available (very old browsers) or you need server-side tracking, use PHP’s built-in upload progress or a PECL extension (uploadprogress) and poll a small status endpoint from the main page. Note: some server setups buffer request bodies (or certain FastCGI configurations), which prevents the server from reporting intermediate progress until the upload finishes — check your PHP/webserver config if progress stays empty. (php.net)

Quick troubleshooting: open Firefox DevTools → Network and watch the timing columns while uploading; if the iframe request shows long “Blocked” time you’ve confirmed the browser-queue explanation.

Recommended Answers

All 6 Replies

try:

header("refresh: 1; url=" . $_SERVER['PHP_SELF']);
echo substr(time(),-2,2);

doesnt work, why would it? Doesnt really make a difference.

Please help. Thanks.

why would it?

You were not including the "url=" part. INSTEAD of the header() function, you can try:

echo '<script>location.href="'.$_SERVER['PHP_SELF'].'?cb=(new Date()).valueOf()";</script>';
exit();

Well, I need to display percent done.

So, new code is:
(help me fix this)

header("Cache-Control: no-cache \n");
header("Pragma: no-cache \n");
header("Expires: 0 \n");
header("Refresh:3; $_SERVER[PHP_SELF]?sid=$sessionid");
//echo '<script>location.href="'.$_SERVER.'?sid=(<?php echo $sid; ?>).valueOf()";</script>'; //NOT REQUIRED!
echo $percent_done.'% completed';

try:

header("Cache-Control: no-cache \n");
header("Pragma: no-cache \n");
header("Expires: 0 \n");
//header("Refresh:3; $_SERVER[PHP_SELF]?sid=$sessionid");

//not sure if the variable with the id that you are using is $sid OR $sessionid, but be sure to use the correct one below
echo sprintf('<script>setTimeout( function(){location.href="%s?sid=%s&cb=%s"} ,3000);</script>',$_SERVER['PHP_SELF'], $sessionid, time());
echo $percent_done.'% completed';


Woo! It does work! THANKS A LOTTTTTT MAN!

Cheers & my best wishes!

See ya...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.