0

我正在使用 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 不提示下载。

4

1 回答 1

1

目前,您正在尝试这样做:

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);
于 2013-01-19T14:07:04.523 回答