[ZipStream]using zipstream downloading many urls

234 Views Asked by At

I have tried to use ZipStream and I figured that when the urls are small like 5 urls,it works well,but when the urls are bigger, the brower turns around and around and I can't get the zip file.Has anybody met same question before? Many thanks.Below is my code.I'm using mac os and debug the code on phpstorm using localhost and port 63342.

<?php
require_once __DIR__ . '/vendor/autoload.php';

use ZipStream\ZipStream;

// Create new Zip
$zip = new ZipStream("hi.zip");

// add a lot of http urls
$urls = [
'http://img31.mtime.cn/pi/2014/10/22/092931.12614666_1000X1000.jpg',
'http://img31.mtime.cn/pi/2014/10/22/092931.79193700_1000X1000.jpg',
];

foreach($urls as $url) {
 // Create Temp File
  $fp = tmpfile();

// Download File
$ch = curl_init();
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_URL, $url);
curl_exec($ch);

// Force to write all data
fflush($fp);

// Rewind to the beginning, otherwise the stream is at the end from the start
rewind($fp);

// Find out a file name from url
// In this case URL 
http://img31.mtime.cn/pi/2014/10/22/092931.12614666_1000X1000.jpg will yield
// /pi/2014/10/22/092931.12614666_1000X1000.jpg as file path
$filename = parse_url($url, PHP_URL_PATH);

// Add File
$zip->addFileFromStream($filename, $fp);

// Close the Temp File
fclose($fp);
}

// Finish ZIP
$zip->finish();
1

There are 1 best solutions below

0
lily On

well,I have succeffully downloaded 1.3G zip file from the brower using the above code. At first, I can't get the zip file because I use phpstorm inner Interpreter,later I changed to apache server and successed. Thanks for zipstream's author Jonatan Männchen.