Use PHP and PEAR to dynamically create and edit the TAR document-PHP source code

Source: Internet
Author: User
Ec (2); if you have downloaded widgets for your UNIX system, you must have encountered TAR compressed files. In UNIX systems, the tape archive file format is often used to package multiple files for kvm distribution. This format is popular not only because it exists in all UNIX systems, but also because in practical applications, it stores the directory structure, license information, user and group information of archive files, and other office buildings. PHP has a large number of built-in functions and connectable extension libraries, which can be used in almost all file formats: script ec (2); script




If you have downloaded widgets for your UNIX system, you must have encountered TAR compressed files. In UNIX systems, the tape archive file format is often used to package multiple files for kvm distribution.

This format is popular not only because it exists in all UNIX systems, but also because in practical applications, it stores the directory structure, license information, user and group information of archive files, and other office buildings.

PHP has a large number of built-in functions and connectable extension libraries, which can be used in almost all file formats. In particular, you can use the Archive_Tar class of PEAR to read and create TAR documents for notebook repairs. In this article, we will explain 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 installed PHP, And you have correctly installed PEAR's Archive_Tar class.

Note: You can directly install the PEAR Archive_Tar package from the Internet or download and install it from the Internet.

Create a TAR document

Let's start with a simple example: dynamically create TAR documents containing several files. Use the following script:
List

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 '';
}
>

This script is quite simple, but it is worth looking:

First, create an instance of the Archive_Tar class and initialize the path and file name of the created document. In this example, the file name is dummy.tar, Which is saved in the current directory of Beijing Induction Cooker.

Next, list all the archived files, including their disk addresses, and store them in a PHP array. The script (more accurately, the user who runs the script) must have the permission to access these disk addresses. Otherwise, the document creation process will fail.

Finally, the creat () method compresses all files to a single TAR file. This method accepts the input array of file names and returns a Boolean value indicating whether the document is successfully created. Remember that the script must have the permission to write files to the named disk address. Otherwise, the creat () method cannot be used, and the document creation will fail to be performed by huatai Santa Fe.

.

View TAR document content

How can 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
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. Each array element of the Temperature Controller represents a file in the document. Typically, each element has the file name information, its license mode, owner and group user ID, status, size, and last modification time. Through a loop, you can easily extract the information, and then slightly modify the format to make it according to the expression, as mentioned in the above example. The following is an input example:

Checksum: 3872

File Name: mystuff/alcon.doc

Mode: 33206

ID: 433

ID: 106

Size: 41472

Time: 1140358031

Add a file to an existing TAR file

Because of the forgetful nature of all of us, we may sometimes forget a file. The Archive_Tar class provides
Ability to add new files to existing documents

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.