John Davidson

javascript - Create a folder in downloads and Download multiple images from external server on single click in it

0 comments
Message:


I want to download multiple images on single click.
I have external links of the images.


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


Any method which will create a folder in my PC Downloads and save all images in it.
I have HostGator Linux dedicate server hosting with cPanel


My app is based on PHP 7/JS/jQuery/Bootstrap.



My Answer:
You can achieve this using JavaScript and PHP. Here is a sample code snippet to create a folder in the Downloads directory and download multiple images from external server on a single click:

1. Create a PHP script to download the images:

php
$images = 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'
);

$folder = 'Downloads/images';
if (!file_exists($folder)) {
mkdir($folder, 0777, true);
}

foreach ($images as $image) {
$imageData = file_get_contents($image);
$fileName = basename($image);
$filePath = $folder . '/' . $fileName;
file_put_contents($filePath, $imageData);
}

echo 'Images downloaded successfully!';
?>


2. Create a JavaScript function to trigger the download:

javascript
function downloadImages() {
$.ajax({
url: 'download_images.php',
success: function(response) {
alert(response);
}
});
}


3. Add a button in your HTML file to trigger the download:

html



When you click the button, it will trigger the PHP script to download the images from the external server and save them in the Downloads/images folder on your server. You can then provide a link to download the folder containing the images to your users.

Rate this post

3 of 5 based on 1341 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us