• February 12, 2023

    example PHP code that loads images from a list of URLs and displays the current file loading in real-time and display progress bar while process running

    add function , for click button before process

    <?php
    if (isset($_POST['load-images'])) {
        loadImages();
    }
    
    function loadImages() {
        $url_list = [
            "https://example.com/image1.jpg",
            "https://example.com/image2.jpg",
            "https://example.com/image3.jpg"
        ];
    
        $total_count = count($url_list);
        $current_count = 0;
    
        ob_end_flush();
        ob_implicit_flush(true);
    
        foreach ($url_list as $url) {
            $current_count++;
            $filename = basename($url);
    
            echo "Loading $url ($current_count/$total_count)<br>";
    
            $ch = curl_init($url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, function ($resource, $download_size, $downloaded, $upload_size, $uploaded) use ($url, $current_count, $total_count, $filename) {
                if ($download_size > 0) {
                    $percent = $downloaded / $download_size * 100;
                    printf("Loading %s (%d/%d): %d%% (%s)<br>", $url, $current_count, $total_count, $percent, $filename);
                    flush();
                }
            });
            $data = curl_exec($ch);
            curl_close($ch);
    
            file_put_contents($filename, $data);
    
            echo "Finished loading $url<br>";
            flush();
        }
    }
    ?>
    
    <html>
    <body>
        <form method="post">
            <button name="load-images">Load Images</button>
        </form>
    </body>
    </html>
    


เวอไนน์ไอคอร์ส

ประหยัดเวลากว่า 100 เท่า!






เวอไนน์เว็บไซต์⚡️
สร้างเว็บไซต์ ดูแลเว็บไซต์

Categories


Uncategorized