How do I create a compressed RAR file in PHP?

Source: Internet
Author: User
How do I create a compressed RAR file in PHP?

In the Web site, most of the uploaded files are in the form of compressed package, so we first have to upload the files before we can upload, then how do we implement the Web site to upload files automatically compressed function? Let's give you a detailed introduction today!

File compression is mainly applied to PHP's predefined class COM, the Wscrip.shell object as the parameters of the COM class, and finally by calling the COM class's run () method to implement the file compression.

The COM class provides a framework for consolidating COM components into PHP scripts, with the following instantiation methods:

$obj = new COM ("Server.object")

The syntax for a COM class constructor is as follows:

String com::com (String module_name [, String server_name [, int codepage]])

Parameter description:

Module_name: The name or Class-id of the requested component.

Server_name:dcom server name, component is taken on this server, if NULL, then Loaclhost is assumed. If you want to support allowing DCOM, you must set the com.allow_dcom in PHP.ini to TRUE.

CodePage: Specifies the code page used to convert the PHP string (php-string) to a Unicode string (unicode-strings), and vice versa. The available values are CP_ACP, CP_MACCP, CO_OEMCP, Cp_symbol, CP_THREAD_ACP, Cp_utf7, and Cp_utf8.

The syntax format for the run () method in a COM class is as follows:

Run ("WinRAR a generated RAR file path and name to compress the path and name of the file", 1,false);

Application examples

This example explains how to create a RAR file in PHP with the following steps:

(1) Create a PHP page, first create a form, set the file field, submit the file to be compressed, set the text box, submit the compressed file storage location, set the button, complete the form element of the commit operation.

(2) in PHP files to write PHP script, through the Post method to get form submitted data, through the Iconv () function to complete the conversion of data encoding format, instantiate the COM class, call the Run () method to complete the file compression operation.

The code is as follows:

<?phpheader ("content-type:text/html; Charset=utf-8 "), if ($_post[' Submit ') {    $sourcefile = iconv (" Utf-8 "," gb2312 ", $_post[' sourcefile '));  Gets the path to the specified file    $objfile = $_post[' objfile '). " \ \ ". Time ();                    Define the storage location and name of the compressed file    $objfile = Iconv ("Utf-8", "gb2312", $objfile);                  Implementing the encoding conversion    $obj = new COM ("Wscript.Shell");                               Instantiate the COM class    $obj run ("WinRAR a". $objfile. "". $sourcefile. "", 1,false);   Call the method in the class    echo "<script>alert (' File compression complete! ');</script> ";}? >

Run this instance, first select the file to compress, then set the storage location of the compressed file, click the "Submit" button, it is done!

Attention:

When performing a file compression operation, remember that there are no spaces in the file's storage path (including the path of the compressed file, the storage path of the name-level compressed file), or the compression will not execute correctly!

Here we introduce how to create a compressed RAR file in PHP, the small partners have mastered it?

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.