PHP ZipArchive文件URL(Http)

问题描述 投票:0回答:1

所以我有一个脚本,可将文件下载到zip文件中。但这是行不通的。引起我注意的是,您不能对外部URL上的文件使用ziparchive。但是后来我在堆栈上找到了一篇博客文章,说可以。我尝试了该解决方案,但仍然收到一个空的或损坏的zip文件。

他们推荐的解决方案是这个

    // Download file
    $file = "http://url.com/file.pdf";        
    $ch = curl_init($file);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    $file_content = curl_exec($ch);
    curl_close($ch);

    // Add to zip
    $zip->addFromString($fname, $file_content);

我循环运行代码以将所有文件添加到zip。但是即时通讯仍然收到一个空的/损坏的zip文件。谁能帮我将外部文件(http)添加到zip中并下载?谢谢。

php ziparchive
1个回答
0
投票

这样的事情最简单吗?

$pdf = file_get_contents('http://url.com/file.pdf');
$zip->addFromString($fileName, $pdf);
© www.soinside.com 2019 - 2024. All rights reserved.