由于file_put_contents
方法,我下载了一个大.tgz,但我只对.tgz.中的一些文件/目录感兴趣
我计划使用PharData::extractTo
和PharData::decompress
方法来获取其内容,但它们提取了所有文件。
有没有办法提取我想要的精确文件/目录,或者我必须提取所有文件?
您可以使用phar://
流包装器来访问归档文件的单个文件的内容,请参阅使用Phar归档文件上的示例:Phar和PharData类:
<?php // these two calls to file_get_contents() are equivalent if // /path/to/myphar.phar has an explicit alias of "myphar.phar" // in its manifest, or if the phar was initialized with the // previous example's Phar object setup $f = file_get_contents('phar:///path/to/myphar.phar/whatever.txt'); $f = file_get_contents('phar://myphar.phar/whatever.txt'); ?>
例如,以下代码
var_dump(file_get_contents('phar://test.tar/test.sql'));
可能会生成以下输出:
string(94) "INSERT INTO Users(Username) VALUES ('abc');
SELECT * FROM Users;
CREATE TABLE dummy (id INT);
"