javascript - 在下载中创建一个文件夹,并单击从外部服务器下载多个图像
问题描述
我想一键下载多张图片。我有图像的外部链接。
任何将在我的 PC 下载中创建文件夹并将所有图像保存在其中的方法。我有 HostGator Linux 专用服务器托管与 cPanel
我的应用基于PHP 7/JS/jQuery/Bootstrap。
解决方案
<?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--;
}
} ?>
注意:这将省略一些其他异常,例如权限被拒绝,需要以不同方式处理。
推荐阅读
- angular - 我如何在 ionic 3 中制作动态单选按钮以及 altertctl 选项
- php - 如何将输入、文本区域和图像上传插入数据库
- angular - src/app/app.component.ts(20,27) 中的错误:错误 TS2345:'string | 类型的参数 ArrayBuffer' 不可分配给“字符串”类型的参数
- javascript - 使用简化方法在两个给定日期之间生成一系列日期
- hive - 在 hive 中向外部表添加分区需要很多时间
- pandas - 通过屏蔽特定行之间的子集 df
- java - 如何使用 Spring Boot 向应用程序中的所有现有 API 添加新的请求标头
- c - 努力使最长公共子序列代码并行(C)
- datatables-1.10 - 如果列存在,如何添加“columnDefs”?
- r - is.na() 替换所有值而不是 na 值