0

我尝试使用 ZipStream,我发现当 url 像 5 个 url 一样小时,它工作得很好,但是当 url 更大时,浏览器转来转去,我无法获取 zip 文件。有没有人遇到过同样的情况之前的问题?非常感谢。下面是我的代码。我正在使用 mac os 并使用 localhost 和端口 63342 在 phpstorm 上调试代码。

<?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();
4

1 回答 1

0

好吧,我已经使用上面的代码从浏览器成功下载了 1.3G 的 zip 文件。一开始我用phpstorm内部解释器无法获取zip文件,后来我换成apache服务器成功了。感谢 zipstream 的作者 Jonatan Männchen。

于 2017-08-08T08:57:44.097 回答