本文介绍了没有exec()的PHP Untar-gz?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用纯PHP在不使用exec('tar')或任何其他命令的情况下在php中解压缩文件?

How would I untar-gz a file in php without the use of exec('tar') or any other commands, using pure PHP?

我的问题如下:我有一个26mb的tar.gz文件,需要将其上传到我的服务器上并解压缩.我尝试使用net2ftp提取它,但是它不支持tar.gz上传后解压缩.

My problem is as follows; I have a 26mb tar.gz file that needs to be uploaded onto my server and extracted. I have tried using net2ftp to extract it, but it doesn't support tar.gz uncompressing after upload.

我正在使用免费的Web主机,因此它们不允许任何exec()命令,并且它们也不允许访问提示.那么我要如何解开这个东西呢?

I'm using a free web host, so they don't allow any exec() commands, and they don't allow access to a prompt. So how would I go about untaring this?

PHP有内置命令吗?

Does PHP have a built in command?

推荐答案

自PHP 5.3.0起,您无需使用Archive_Tar.

Since PHP 5.3.0 you do not need to use Archive_Tar.

有新的类可用于tar存档: PharData类.

There is new class to work on tar archive: The PharData class.

提取档案文件(使用 PharData::extractTo() 类似于ZipArchive::extractTo()) :

To extract an archive (using PharData::extractTo() which work like the ZipArchive::extractTo()):

try {
    $phar = new PharData('myphar.tar');
    $phar->extractTo('/full/path'); // extract all files
} catch (Exception $e) {
    // handle errors
}

如果您有tar.gz归档文件,则只需在解压缩之前对其进行解压缩(使用 PharData::decompress() ):

And if you have a tar.gz archive, just decompress it before extract (using PharData::decompress()):

// decompress from gz
$p = new PharData('/path/to/my.tar.gz');
$p->decompress(); // creates /path/to/my.tar

// unarchive from the tar
$phar = new PharData('/path/to/my.tar');
$phar->extractTo('/full/path');

这篇关于没有exec()的PHP Untar-gz?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 06:06