我正在使用 curl 从 Facebook url 为授权用户获取照片,对于每张照片,我将其添加到 zip 文件中,以允许用户以 zip 格式下载整个专辑。
if( !isset($_GET['id']) )
die("No direct access allowed!");
require 'facebook.php';
$facebook = new Facebook(array(
'appId' => 'removed',
'secret' => 'removed',
'cookie' => true,
));
if( !isset($_GET['id']) )
die("No direct access allowed!");
$params = array();
if( isset($_GET['offset']) )
$params['offset'] = $_GET['offset'];
if( isset($_GET['limit']) )
$params['limit'] = $_GET['limit'];
$params['fields'] = 'name,source,images';
$params = http_build_query($params, null, '&');
$album_photos = $facebook->api("/{$_GET['id']}/photos?$params");
if( isset($album_photos['paging']) ) {
if( isset($album_photos['paging']['next']) ) {
$next_url = parse_url($album_photos['paging']['next'], PHP_URL_QUERY) . "&id=" . $_GET['id'];
}
if( isset($album_photos['paging']['previous']) ) {
$pre_url = parse_url($album_photos['paging']['previous'], PHP_URL_QUERY) . "&id=" . $_GET['id'];
}
}
$photos = array();
if(!empty($album_photos['data'])) {
foreach($album_photos['data'] as $photo) {
$temp = array();
$temp['id'] = $photo['id'];
$temp['name'] = (isset($photo['name'])) ? $photo['name']:'photo_'.$temp['id'];
$temp['picture'] = $photo['images'][1]['source'];
$temp['source'] = $photo['source'];
$photos[] = $temp;
}
}
?>
<?php if(!empty($photos)) { ?>
<?php
$zip = new ZipArchive();
$tmp_file =tempnam('.','');
$file_opened=$zip->open($tmp_file, ZipArchive::CREATE);
foreach($photos as $photo) {
$url=$photo['source'];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$download_file=curl_exec($ch);
#add it to the zip
$file_added=$zip->addFile($download_file);
}
# close zip
$zip->close();
# send the file to the browser as a download
header('Content-Description: File Transfer');
header('Content-Type: application/force-download');
header('Content-Disposition: attachment; filename="albums.zip"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($tmp_file));
ob_clean();
flush();
readfile($tmp_file);
exit;
} ?>
问题: zip->open 创建一个文件, zip->add 为每个添加的文件返回 1 但 zip 大小仍然为 0 并且 readfile 不提示下载。