首页 > 解决方案 > 在下载中创建一个文件夹,并单击从外部服务器下载多个图像

问题描述

我想一键下载多张图片。我有图像的外部链接

https://focalassetsstagingcdn.azureedge.net/orders2021/971014/photosraw/0ba59d1b-83be-42af-a9dd-f1c10e69f540.jpg

https://focalassetsstagingcdn.azureedge.net/orders2021/971014/photosraw/0dcfb56e-5a25-4fb0-bb9c-dd36ddab561c.jpg

任何将在我的 PC 下载中创建文件夹并将所有图像保存在其中的方法。我有 HostGator Linux 专用服务器托管与 cPanel

我的应用基于PHP 7/JS/jQuery/Bootstrap

标签: javascriptphpjqueryimage

解决方案


<?php
set_error_handler(function($errno, $errstr, $errfile, $errline) { //turn directory not found warning into exception
    if (0 === error_reporting()) {
        return false;
    }       
    throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
});
$imagesarray = array("https://focalassetsstagingcdn.azureedge.net/orders2021/971014/photosraw/0ba59d1b-83be-42af-a9dd-f1c10e69f540.jpg", "https://focalassetsstagingcdn.azureedge.net/orders2021/971014/photosraw/0dcfb56e-5a25-4fb0-bb9c-dd36ddab561c.jpg"); //all the images url that you have in an array
$destination = "downloaded_images/"; //your folder name
$imagecount = sizeof($imagesarray);
for($i=0 ; $i < $imagecount; $i++) {
    try{ //The folder may not exist
        file_put_contents($destination.$i.".jpg", file_get_contents($imagesarray[$i]));
    }
    catch(Exception $e){ //if so create a folder and startover.
        mkdir($destination);
        $i--;
    }
} ?>

注意:这将省略一些其他异常,例如权限被拒绝,需要以不同方式处理。


推荐阅读