Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
350 views
in Technique[技术] by (71.8m points)

PHP - download multiple images to local directory using their URLs

I have written some code that accesses a website's content using cURL and stores the image urls in an array. Now I want to go through this array and download all images to a folder on my computer. How can I achieve this?

This is the code so far gets the images from the website and prints the images on my webpage.

    <?php

        $curl = curl_init();

        $search_string = "pc%20video%20games%202020";
        $url = "https://www.daraz.pk/catalog/?q=$search_string";

        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

        $result = curl_exec($curl);

        preg_match_all("!https://static-01.daraz.pk/p/[^s]*?.jpg!", $result, $matches);

        $images = array_values(array_unique($matches[0])); //array that contains image urls

        for($i=0; $i < count($images); $i++){
            echo "<div style='float: left; margin: 10 10 10 10;'>";
            echo "<img src='$images[$i]' style='width:250px; height:200px;'><br/>";
            echo "</div>";
         }

        curl_close($curl);

      ?>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...