• February 12, 2023

    Here’s an updated version of the code that displays the status of each image as it’s being loaded, as well as a running total of the number of images that have been successfully loaded:

    <?php
    // List of URLs
    $url_list = array(
        'https://example.com/image1.jpg',
        'https://example.com/image2.jpg',
        'https://example.com/image3.jpg',
        'https://example.com/image4.jpg',
        'https://example.com/image5.jpg',
    );
    
    // Destination folder
    $dest_folder = './images/';
    
    // Create destination folder if it doesn't exist
    if (!is_dir($dest_folder)) {
        mkdir($dest_folder);
    }
    
    // Loop through each URL and save the image
    $total_count = count($url_list);
    $loaded_count = 0;
    foreach ($url_list as $url) {
        $image_content = file_get_contents($url);
        if ($image_content !== false) {
            $img = imagecreatefromstring($image_content);
            $filename = $dest_folder . basename($url);
            imagepng($img, $filename);
            imagedestroy($img);
            $status = "Loaded";
            $loaded_count++;
        } else {
            $status = "Failed";
        }
        echo $url . " - " . $status . "<br>";
        echo "Loaded " . $loaded_count . " of " . $total_count . " files.<br>";
        flush();
    }
    ?>
    

    Note that the flush() function is called after each image is processed in order to ensure that the output is immediately sent to the browser. This may not be necessary depending on your server configuration, but it can help ensure that the output is displayed in real time.



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

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






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

Categories


Uncategorized