简体   繁体   English

如何使用 CURL 将原始数据转换为 php 中的 zip 文件?

[英]How to convert raw data into zip file in php using CURL?

I am using php Curl to get zip file but getting the raw data like我正在使用 php Curl 来获取 zip 文件,但获取的原始数据如下

在此处输入图片说明

How to convert that data into zip file I have tried this如何将该数据转换为 zip 文件我已经试过了

$url = "https://wordpress.org/latest.zip"; // URL of what you wan to download
$zipFile = "wordpress.zip"; // Rename .zip file
$extractDir = "extracted"; // Name of the directory where files are extracted
$zipResource = fopen($zipFile, "w");

// Get The Zip File From Server
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
curl_setopt($ch, CURLOPT_FILE, $zipResource);

$page = curl_exec($ch);

if(!$page) {
    echo "Error :- ".curl_error($ch);
}

curl_close($ch);

/* Open the Zip file */
$zip = new ZipArchive;
$extractPath = $extractDir;

if($zip->open($zipFile) != "true"){
    echo "Error :- Unable to open the Zip File";
} 

/* Extract Zip File */
$zip->extractTo($extractPath);
$zip->close();

die('Your file was downloaded and extracted, go check.');

How to convert that raw data into zip file.如何将该原始数据转换为 zip 文件。

I have received that error ** ZipArchive::extractTo(): Invalid or uninitialized Zip object**我收到了那个错误 ** ZipArchive::extractTo(): Invalid or uninitialized Zip object**

如果我将该文件放在一个文件夹中并使其权限为 777,它会出现一些权限问题,它可以完美运行并下载数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM