• February 12, 2023
    <?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
    foreach ($url_list as $url) {
        $image_content = file_get_contents($url);
        $img = imagecreatefromstring($image_content);
        $filename = $dest_folder . basename($url);
        imagepng($img, $filename);
        imagedestroy($img);
    }
    ?>
    

    To use this code on your website, you can create a new PHP file (e.g. load_images.php) and paste the code above into it. Upload the file to the root directory of your website, or to a subdirectory if you prefer, and then navigate to the file in your web browser (e.g. https://vir9.com/loadx/load_images.php). The images will be downloaded from the URLs in the $url_list and saved to the $dest_folder that you specified. Note that you may need to adjust the file permissions of the destination folder to allow write access by the PHP script.

    ให้โหลดไฟล์รูปมาที่เดียวกับไฟล์ที่ใช้รัน

    To change the destination folder to the same directory as the PHP file you’re running, you can set the $dest_folder variable to the path relative to the current working directory by using dirname(__FILE__). Here’s the updated code:

    <?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 (in the same directory as this PHP file)
    $dest_folder = dirname(__FILE__) . '/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
    foreach ($url_list as $url) {
        $image_content = file_get_contents($url);
        $img = imagecreatefromstring($image_content);
        $filename = $dest_folder . basename($url);
        imagepng($img, $filename);
        imagedestroy($img);
    }
    ?>
    This will create an images directory in the same directory as the PHP file and save the downloaded images to that folder.

    โชว์สถานนะเมื่อโปลดเสร็จ

    <?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
    foreach ($url_list as $url) {
        $status = '';
        $headers = get_headers($url, 1);
        if ($headers !== false && strpos($headers[0], '200') !== false) {
            $image_content = file_get_contents($url);
            $img = imagecreatefromstring($image_content);
            $filename = $dest_folder . basename($url);
            imagepng($img, $filename);
            imagedestroy($img);
            $status = 'Loaded';
        } else {
            $status = 'Could not load';
        }
        echo "URL: $url Status: $status<br>";
    }
    ?>
    

    Yes, you can display real-time progress while loading images using PHP. One way to achieve this is by using JavaScript and AJAX to update the progress bar as each image is loaded.

    Here’s an example code that you can use as a starting point:

    <!-- HTML code -->
    <div id="progress-bar" style="width: 0%;"></div>
    <div id="result"></div>
    
    <script>
    // JavaScript code
    function loadImage(url) {
      var xhr = new XMLHttpRequest();
      xhr.onreadystatechange = function() {
        if (this.readyState === 4 && this.status === 200) {
          // Image loaded successfully
          document.getElementById("result").innerHTML += "<p>" + url + " loaded.</p>";
          var progress = parseInt(document.getElementById("progress-bar").style.width) + 100 / <?php echo count($url_list); ?>;
          document.getElementById("progress-bar").style.width = progress + "%";
        } else if (this.readyState === 4) {
          // Image failed to load
          document.getElementById("result").innerHTML += "<p>" + url + " failed to load.</p>";
          var progress = parseInt(document.getElementById("progress-bar").style.width) + 100 / <?php echo count($url_list); ?>;
          document.getElementById("progress-bar").style.width = progress + "%";
        }
      };
      xhr.open("GET", url, true);
      xhr.send();
    }
    
    // Loop through each URL and load the image
    <?php foreach ($url_list as $url) { ?>
      loadImage("<?php echo $url; ?>");
    <?php } ?>
    </script>
    

    This code creates a progress bar that is initially set to 0%, and a result div that will display whether each image has loaded successfully or failed to load. The loadImage() function is called for each URL, which sends an AJAX request to load the image. If the request is successful, the progress bar is updated and a success message is displayed. If the request fails, the progress bar is still updated and a failure message is displayed.

    Note that this code uses JavaScript and AJAX to achieve real-time progress updating, and it assumes that the HTML and JavaScript code are included in the same file as the PHP code. If you want to use this code in a separate JavaScript file, you will need to make some adjustments to pass the URLs to the JavaScript file.



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

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






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

Categories


Uncategorized