我使用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不提示下载。
发布于 2013-01-19 14:07:04
现在,你正在尝试这样做:
foreach ($photos as $photo) {
$url = $photo['source'];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$download_file = curl_exec($ch);
$file_added = $zip->addFile($download_file);
}这将无法工作,因为$dowload_file是字符串,而不是文件名。您需要将cURL参数保存到文件中,并将文件名传递给$zip->addFile。如果您查看PHP文档,它需要的是一个文件名,而不是字符串:
要添加的文件的路径。
你需要做的事情是:
$temp_file = './temp_file.txt';
file_put_contents($temp_file, $download_file);
$file_added = $zip->addFile($temp_file);
// Delete after use
unlink($temp_file);https://stackoverflow.com/questions/14414715
复制相似问题