How to extract tar.gz format files in PHP _php tips

Source: Internet
Author: User
Tags exception handling extract gz pear php error

This article illustrates the way PHP extracts tar.gz format files. Share to everyone for your reference, specific as follows:

1, the use of PHP with compression and archive expansion (Phar)

$phar = new Phardata (' song.tar.gz ');
Path to extract whether the file overwrites
$phar->extractto (' c:/tmp ', NULL, true);

2, the first need to install Pear class management tools

Linux under

/usr/local/php/bin/pear Install Archive_tar

Wamp default does not install pear, go-pear.php for older versions, php5.3 need to download Go-pear.phar

Put the downloaded file in the PHP directory (can be under any directory) to execute the PHP go-pear.phar command, press ENTER according to the prompts

①, installation

#这个类实际安装pear时默认就会安装
Pear Install Archive_tar

Ii

/**
principle: Tar compression package before 512 bytes have their own fixed format, according to these 512 bytes can get the file name of the compression package, size and other information
in conjunction with Gzopen,gzread can extract GZ format
 * * *
$tar _ Object = new Archive_tar ("song.tar.gz");
$tar _object->extract (' c:/tmp ', true);

3, PHP calls the external command to perform decompression

The script will not be launched until the command is executed, and the Popen command is not asynchronous to execute
error_reporting (e_all);
Ini_set ("Display_errors", 1);
$handle = Popen ("TAR-XZVF song.tar.gz 2>&1", ' R ');
/* While
(!feof ($handle)) {
set_time_limit (3);
  $buffer = fgets ($handle);
  echo "$buffer \ n";
  Ob_flush ();
  Flush ();
} * *
echo "start \ n";
Pclose ($handle);
echo "End \ n";

For more information about PHP interested readers can view the site topics: "PHP operation zip file and compression skills summary", "PHP basic Grammar Introductory Course", "PHP error and Exception handling method summary" and "PHP common functions and Skills summary"

I hope this article will help you with the PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.