Create zip with PHP adding files from url [closed]
Asked Answered
T

2

18

I was wondering if the following is possible to do and with hope someone could potentially help me.

I would like to create a 'download zip' feature but when the individual clicks to download then the button fetches images from my external domain and then bundles them into a zip and then downloads it for them.

I have checked on how to do this and I can't find any good ways of grabbing the images and forcing them into a zip to download.

I was hoping someone could assist

Trephine answered 18/12, 2012 at 9:38 Comment(2)
You are describing quite a complex set of operations - you're question is not specific. What part of this are you having problems with? What have you tried?Reitz
I was more curious if there is such a way to download images from external domain into a zip file. I'm currently researchign possibilitiesTrephine
L
69
# define file array
$files = array(
    'https://www.google.com/images/logo.png',
    'https://en.wikipedia.org/static/images/project-logos/enwiki-2x.png',
);

# create new zip object
$zip = new ZipArchive();

# create a temp file & open it
$tmp_file = tempnam('.', '');
$zip->open($tmp_file, ZipArchive::CREATE);

# loop through each file
foreach ($files as $file) {
    # download file
    $download_file = file_get_contents($file);

    #add it to the zip
    $zip->addFromString(basename($file), $download_file);
}

# close zip
$zip->close();

# send the file to the browser as a download
header('Content-disposition: attachment; filename="my file.zip"');
header('Content-type: application/zip');
readfile($tmp_file);
unlink($tmp_file);

Note: This solution assumes you have allow_url_fopen enabled. Otherwise look into using cURL to download the file.

Lydia answered 18/12, 2012 at 9:53 Comment(12)
I receive the following "Windows cannot open the folder. The Compressed (zipped Folder 'C:\download.zip' is invalidTrephine
@DonaldSutherland see my edit. I did some stupid stuff with variable names.Lydia
Thanks for the code.. Anyway to also download folders/directories?Prithee
@Mr_Green, you can't really do that unless directory listing is enabled on the server, and even then you'd have to parse the HTML to get all the files in each folder.Lydia
@Lydia please have a look at this link.Prithee
@Lydia thank you sir for the solution, but how do we clear the temp file after the download progress?Grater
You should add unlink($tmp_file) after you read the file so it doesn't clutter up hosting spaceGauhati
thanks buddy. worked like a charm...Pay
Zip is downloaded successfully but when I extract it thrown Archive is corrupted error. I have tried with the same example without any change. Still facing the same issueBlastula
that's probably because http://google.com/images/logo.png no longer works. i've updated the examples to use https and fixed the 404 url.Lydia
Error "Windows cannot open the folder. The Compressed (zipped Folder 'C:\download.zip' is invalid" still going onFiredrake
probably because the wikipedia image no longer works - i've updated the exampleLydia
N
2

I hope I didn't understand wrong.

http://php.net/manual/en/book.zip.php

I haven't tried this, but it seems like what you're looking for.

<?php
$zip = new ZipArchive;

if ($zip->open('my_archive.zip') === TRUE) {
    $zip->addFile($url, basename($url));
    $zip->close();
    echo 'ok';
} else {
    echo 'failed';
}
?>
Na answered 18/12, 2012 at 9:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.