PHP解压字符串 [英] PHP unzip string
本文介绍了PHP解压字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在通过 API 将 Google Drive 电子表格加载到 PHP 中.该请求返回 XLSX
电子表格,我需要解压缩它.为了节省我将响应写入临时文件然后调用,例如 zip_open()
,有没有一种方法可以将这样的方法传递给一个字符串?
I'm loading in a Google Drive spreadsheet via the API into PHP. The request returns the XLSX
spreadsheet and I need to unzip it. To save me writing the response to a temporary and then calling, say, zip_open()
, is there a way I can just pass such method a string?
推荐答案
我认为最好的选择是创建一个临时文件,然后解压它.
I think your best option is to create a temporary file and then to unzip it.
// Create a temporary file which creates file with unique file name
$tmp = tempnam(sys_get_temp_dir(), md5(uniqid(microtime(true))));
// Write the zipped content inside
file_put_contents($tmp, $zippedContent);
// Uncompress and read the ZIP archive
$zip = new ZipArchive;
if (true === $zip->open($tmp)) {
// Do whatever you want with the archive...
// such as $zip->extractTo($dir); $zip->close();
}
// Delete the temporary file
unlink($tmp);
这篇关于PHP解压字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文