【问题标题】:How can I download multiple images from URL using PHP and save them on my folder?如何使用 PHP 从 URL 下载多个图像并将它们保存在我的文件夹中?
【发布时间】:2019-05-08 02:14:48
【问题描述】:

我想从不同的 URL 获取图像并将它们保存在我当前的文件夹中,所以我以后无法获取它们,这是我的代码: 在我的#upload.html 中我做了这个:

<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>

在我的#upload_image.php 中,我编写了以下代码:

<?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'>"; } ?>

顺便说一句,这是我第一次来这里,谢谢你的回答

【问题讨论】:

    标签: php image url


    【解决方案1】:

    这对你有好处吗? 您将图像 URL 放在一个 textarea 中,然后获取 textarea 行并循环获取图像。 我还添加了一个随机数以具有不同的图像名称,但您可以在文件名中添加任何您想要的内容

    upload_image.php 中的 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 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>
    

    【讨论】:

    • 非常感谢您的回答,但就我而言,我希望当我从 url 放置一个链接时,我会获取图像并将其保存在我的文件夹中,并使用不同的名称。
    • 我不明白你想要达到什么目的......我的脚本做到了......如果你能更好地解释你的需求,我可以更正答案
    猜你喜欢
    • 2012-04-04
    • 1970-01-01
    • 2012-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-06
    • 1970-01-01
    • 2019-11-18
    相关资源
    最近更新 更多