Dynamically create and edit tar documents using PHP and Pear

Source: Internet
Author: User
Tags foreach pear



If you've ever downloaded widgets for your UNIX system, you must have encountered the tar compression file. The tape archive file format is often used in UNIX systems to package multiple files to facilitate the distribution of KVM.

This format is popular not only because of the creation tool for this format in all UNIX systems, but also because it facilitates the storage of office buildings such as the directory structure, license information, user and group information of the archive in practical applications.

PHP has a large number of built-in functions and can be connected to the extension library, can be used in almost all file formats. Especially notebook repair It can read and create the TAR document through the Archive_tar class of pear. In this article, we'll show you the behavior of this class and demonstrate how developers can use it to dynamically create, edit, and display tar documents.

Here I will assume that you have a working database and PHP installed, and that the Archive_tar class of pear is installed correctly.

Note: You can install Pear's Archive_tar package directly from the Internet or download it from the Internet.

Create a tar document

Let's start with a simple example: dynamically creating a tar document that contains several files. Use the following script:
List A

<php
Include (' archive/tar.php ');//Import class

$obj = new Archive_tar (' Dummy.tar '); Name of archive

$files = Array (' Mystuff/ad.gif ',
' Mystuff/alcon.doc ',
' Mystuff/alcon.xls '); Files to store in archive

if ($obj->create ($files)) {
Echo ' Created successfully! ';
} else {
Echo ' Error in file Creation ';
}
>

The script is fairly simple, but it's worth looking at:

First, create an instance of the Archive_tar class, initializing the path and file name of the document that was created. In this case, the filename is dummy.tar, stored in the current directory of the Beijing electromagnetic stove.

Second, list all files that are archived, including their disk addresses, in a PHP array. The script (or, more accurately, the user running the script) must have permission to access these disk addresses, or the document creation process will fail.

Finally, the creat () method compresses all the files into a single tar document. This method takes an array of input file names and returns a Boolean value indicating whether the document was successfully created. Remember that the script must have permission to write the file to the named disk address, otherwise the creat () method will not work and the document creation fails. Huatai Santa Fe.

Using the script in the example above, modify the contents of the $files array to reflect your local system configuration, try to execute it on the command line or in the browser, and if everything works, the script will find and read the files to a tar document named Dummy.tar.

View the contents of the TAR document

How do I view the internal information of an existing tar file? The Archive_tar class has a listcontent () method that allows you to view the details of each component in a tar package. List B shows how to use it:

List B
<php
Include (' archive/tar.php ');//Import class

$obj = new Archive_tar (' Dummy.tar '); Name of archive

$files = $obj->listcontent ();//array of file information

foreach ($files as $f) {
foreach ($f as $k => $v) {
echo "$k: $VN";
}
echo "n";
}
>

The output of the Listcontent () method is a structured array, and the temperature controller each array element represents a file in the document. Typically, each element has the name information of the corresponding file, its license mode, the ID of the owner and group user, the status, the size, and the time of the last modification. It is easy to extract this information through a loop, and then modify the formatting slightly so that it is based on the expression, as mentioned in the above example. Here is an example of an input:

Checksum: 3872

FileName: Mystuff/alcon.doc

Mode: 33206

User id:433

Group id:106

Size: 41472

Time: 1140358031

Add a file to an existing tar document

Due to our forgetful domestic tourism, sometimes we may forget a file, the Archive_tar class provides a way through the Add () method
Ability to add new files to a document that already exists

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.