I’d like to force my code to pause after completion and before displaying a completion message. I’ve tried sleep and wait commands but they affect the start of running the code. I’ve searched the web for how to do this for two days with no success. The reason for wanting the delay before showing the completion message is that the code processes so quickly that the completion message appears as soon as the page shows on the computer. I’d appreciate some help on this. Here’s my code:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, height=device-height, initial-scale=1.0">
  <title>Foxclone backup, restore, and cloning utility</title>  
  <meta name="description" content= "FoxClone is a Linux based image backup, restore and clone tool using a simple point and click interface." />

    <link rel="stylesheet" type="text/css" media="screen" href="css/update.css"> 

    <!-- Favicon  -->
    <link rel="apple-touch-icon" sizes="180x180" href="images/favicon/apple-touch-icon.png">
    <link rel="icon" type="image/png" sizes="32x32" href="images/favicon/favicon-32x32.png">
    <link rel="icon" type="image/png" sizes="16x16" href="images/favicon/favicon-16x16.png">

    <link rel="mask-icon" href="images/favicon/safari-pinned-tab.svg" color="#5bbad5">
    <meta name="msapplication-TileColor" content="#da532c">
    <meta name="theme-color" content="#ffffff">

</head>
<body>

 <div class="container"> 
     <div class="row" style="text-align:center;" >
       <h1>Foxclone Filelist Updating</h1>
     </div>
     <div class="header">
 </div>   
<?php


    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);

    $php_scripts = '../php/';
    require $php_scripts . 'PDO_Connection_Select.php';

    if (!$pdo = PDOConnect("foxclone_data"))     {  
        echo( 'Connection Failed');
    }

        $meg = 1048576 ;  // needed to convert filesize bytes to Mb
        $stmt = $pdo->prepare("UPDATE `files` SET `filename` = ?, `md5` = ?, `filesize` = ?,`logtime` = now() WHERE `id` = ?") ;
        $path = 'download/';  
        $id = NULL;
        $filename = '';

        foreach(glob($path.'*.*') as $file) {
          $id = NULL; 
          $filename = basename($file) ;
          $file = new SplFileInfo($file);
          $extension  = $file->getExtension(); 
          $test = substr($filename, 0,12);
            if ($test == 'foxclone_std') {
                $id = 1;
            }

          if (substr($filename, 0,9) == "foxcloneV"){
                $id = 2;
             }

          if ($extension == "deb") {
                 $id = 3;
            }

          if ($extension == "gz") {
                   $id = 4 ;
            }

          if (substr($filename, 0,4) == "news") {
                   $id = 5 ; 
            }

          if (substr($filename, 0,13) == "foxclone_edge") {
                   $id =6;
            }
            $bytes = number_format(filesize($file) / 1048576, 2) ;
            $getdata = (array($filename, md5_file($file),$bytes , $id)) ;

            $count = $stmt->execute ($getdata) ; 
               IF ($count == 0) {
                echo("Failed to update ".$file);
                die;
        }

    }


   ?>
<-- Here's where I want to pause before displaying the successful completion message -->
<div class="row" style="text-align:center;" </div>   
    <h1>Database Update Successful</h1>

Dani AI

Generated

Two practical ways to get the visible "Database Update Successful" to appear after a pause: (1) keep the server work separate and let the browser delay the message with a small client-side timer (simplest and most reliable), or (2) stream partial output from PHP and then update the page when the server finishes (works but depends on server/proxy settings). and pointed at flushing and buffering; noted a JavaScript approach. The snippets below show both patterns and give common troubleshooting steps.

<div id="status">Processing...</div>

<script>
  // show success after 2 seconds (2000 ms)
  setTimeout(function() {
    document.getElementById('status').textContent = 'Database Update Successful';
  }, 2000);
</script>

For in-place server streaming (push an initial page, do work, then send an update), disable PHP output buffering and compression, force some bytes to the client so the browser starts rendering, then flush and continue. Example pattern:

<?php
while (ob_get_level()) ob_end_flush();
@ini_set('zlib.output_compression', 'Off');
header('Content-Type: text/html; charset=UTF-8');
echo '<div id="status">Processing...</div>';
echo str_repeat(' ', 4096); // encourage early render
flush();

// perform file processing / DB work here

// when done, send a small client-side update
echo "<script>document.getElementById('status').textContent = 'Database Update Successful';</script>";
?>

Notes and troubleshooting: many proxies and gzip compressors buffer responses; add server hints like header('X-Accel-Buffering: no') for nginx or disable mod_deflate gzip for this endpoint if necessary. The str_repeat(…,4096) trick nudges some browsers to render earlier. For true progress updates or long tasks, use a background job + polling (fetch to a status endpoint) or Server-Sent Events/WebSockets instead of blocking PHP workers. Avoid using sleep() server-side to delay the visible message, since that simply holds the server process and is not user-friendly.

Recommended Answers

All 6 Replies

sleep() is the right approach, but you have to play with flushing the output buffer to force what should be echo’ed to the browser up to that point to the end-user. Sorry, I wish I could type more but I’m on my phone and not home.

OK, I'm near a computer now and can more properly answer this question. Basically PHP has an output buffer, which holds onto everything meant to be sent to the web browser all at once (at the completion of the script), instead of sending it as it goes.

That's why when you call sleep(), it doesn't do what you want. The content you've echo'ed to the screen before the sleep() call is waiting in the output buffer for the PHP script to completely finish (which includes processing sleep()), and then everything it sent to the web browser all at the end from the output buffer.

Here's some more information:

https://www.php.net/manual/en/function.flush.php

You may need to use runtime Apache/nginx configurations to turn off output buffering and disable gzip.

commented: "So there's a chance?" - Lloyd Christmas +0

I used a small javascript script to accomplish what I wanted. See 4/1/2022 post.

The javascript method is much more elegant of a solution IMHO. :)

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.