skip to Main Content

I am executing a Python file, that saves images to folders called ‘latestimage.png’ to a folder, in a PHP web page using this:

$proc = popen('python -u python.py', 'r');
                    echo '<pre>';
                    while (!feof($proc))
                        {
                            echo fread($proc, 4096);
                        }
                    echo '</pre>';
            }

As you can see there is a while loop that stops anything else from happening until the script is finished executing. I have a DIV that shows the latest version of ‘latestimage.png’:

<?php
                $filepath = $_SESSION['filepath'];
                $new_file = $filepath ."\latestimage.png";
                ?>
                <img src="<?php echo $new_file; ?>">            
        </div>

I want to know how to dynamically update the DIV so that every time the Python file creates a new image, the DIV is updated to show the new image. Thanks.

2

Answers


  1. Since is part of HTML and HTML is only looked at on the client-side you going to need to use something like AJAX that will request the image from the server for you just given the URL of it. You will have to do this every so often so you know that you are getting the latest version of the photo

    Login or Signup to reply.
  2. try this

    <?php
    $filepath = $_SESSION['filepath'];
    $new_file = $filepath ."\latestimage.png";
    if (isset($_GET['GetIMG'])&& $_GET['GetIMG']==1){
    echo$new_file;
    }else{
    ?>
    <img id="IMG"src="<?php echo $new_file; ?>"> 
    
    <script>
    function GetIMG() 
    {
      var xhttp = new XMLHttpRequest();
    
      xhttp.onreadystatechange = function () 
      {
    
     if (xhttp.readyState === 4 && xhttp.status === 200)
     {
       document.getElementById("IMG").src = xhttp.responseText;
    
        }
      };
    
      xhttp.open("GET", "getIMG.php?GetIMG=1", true);
      xhttp.send();
    }
    
    intrv01 = window.setInterval(GetIMG, 5000); 
    
    </script>
    
    
    <?}?>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search