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>

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.