Is it possible to upload photos from .zip links from a json file ?

Step 1: Download the .zip file from the URL in the JSON file.
Step 2: Extract the contents of the .zip file.
Step 3: Upload (or move) the extracted image files to your desired location.

1. Download and Parse the JSON File​

PHP:
<?php
// Example JSON file with a link to a .zip file
$json_url = 'path/to/your/json/file.json';

// Fetch the JSON data
$json_data = file_get_contents($json_url);

// Decode JSON data to a PHP array
$data = json_decode($json_data, true);

// Assuming the JSON contains a key with the .zip file URL
$zip_url = $data['zip_file_link']; // Change according to your JSON structure

// Download the .zip file from the URL
$zip_file = 'temp.zip'; // Temporary file to store the downloaded .zip
file_put_contents($zip_file, fopen($zip_url, 'r'));
?>

2. Extract the ZIp file​

Code:
<?php
$zip = new ZipArchive;
$res = $zip->open($zip_file);

if ($res === TRUE) {
    // Specify the extraction path
    $extract_to = 'path/to/extract/';

    // Extract all contents of the .zip file
    $zip->extractTo($extract_to);
    $zip->close();
    echo 'Zip extracted successfully';
} else {
    echo 'Failed to open the zip file';
}
?>

3. Upload Photos (Move to Desired Location)​

Code:
<?php
// Directory where the photos were extracted
$extracted_files_dir = 'path/to/extract/';

// Get all files in the extracted directory
$files = glob($extracted_files_dir . '*.{jpg,png,gif}', GLOB_BRACE);

// Loop through the files and move them to the uploads directory
foreach ($files as $file) {
    // Define the destination directory for uploaded photos
    $upload_dir = 'path/to/uploads/';

    // Move each file to the uploads directory
    $file_name = basename($file);
    if (rename($file, $upload_dir . $file_name)) {
        echo "Successfully uploaded: " . $file_name . "<br>";
    } else {
        echo "Failed to upload: " . $file_name . "<br>";
    }
}
?>
 
Back
Top