Rastda
Rastda

Reputation: 13

How can I download multiple images from URL using PHP and save them on my folder?

I want to get images from different URL and save them on my current folder, so I cant get them later, here is my code : in my #upload.html I made this :

<html>
<body>
<div>
<form method="post" action="upload_image.php">
<input type="text" name="img_url" placeholder="Enter Image URL">
<input type="submit" name="get_image" value="Submit">
</form>
</div>
</body>
</html>

and in my #upload_image.php I made this following code:

<?php
if(isset($_POST['get_image']))
{
$url=$_POST['img_url'];
$data = file_get_contents($url);
$new = 'new_image.jpg';
file_put_contents($new, $data);
echo "<img src='new_image.jpg'>"; } ?>

And by the way guys its my first time here, thanks for answering

Upvotes: 1

Views: 1923

Answers (1)

DaFois
DaFois

Reputation: 2223

Is this good for you? you put the image URL one for row in a textarea then get the textarea rows and cycle to get the images. I also added a random number to have different image names but you can put whatever you want in the filename

php in upload_image.php

<?php 
if(isset($_POST['get_image']))
{
    $text = trim($_POST['images_url']);
    $textAr = explode("\n", $text);
    $textAr = array_filter($textAr, 'trim'); // remove any extra \r characters left behind

    foreach ($textAr as $url) {
        $data = file_get_contents($url);
        $new = 'new_image_'.rand(10, 3000).'.jpg';
        file_put_contents('upload/'.$new, $data);
        echo '<img src="upload/'.$new.'">'; 
    } 

} 
?>

HTML form

 <form method="post" action="upload_image.php">
     <textarea name="images_url" placeholder="insert one url per row"></textarea>
      <input type="submit" name="get_image" value="Submit">
 </form>

Upvotes: 1

Related Questions