Use the APC module in PHP to implement the File Upload progress bar

Source: Internet
Author: User
Tags apc apc configuration apc module file upload progress bar

The APC module cannot be used in versions earlier than php5.2, because this APC module is not available in the past. If you want to use the APC module to implement the upload progress bar, you must use php5.2 or a later version.

Starting from 5.2, APC added a new project called APC_UPLOAD_PROGRESS, which solved the long-standing progress bar problem. In addition, it caches all the temporary files during the original upload to the memory and saves them to the hard disk automatically when the temporary files reach the set value, effectively improving the memory utilization.

The function is to assign each upload a unique ID during the upload process. When the PHP script receives an upload file, the interpreter will automatically check the hidden field named APC_UPLOAD_PROGRESS in the $ _ POST array, which will become a cache variable and store information about the upload, in this way, the script can access the status information of the uploaded file through the upload ID.

APC is short for Alternative PHP Cache, which is a free and public Optimized Code Cache of PHP. It is used to provide free, public, and robust architectures to cache and optimize PHP intermediate code.

APC module parameter configuration

The Code is as follows: Copy code

Name Default Changeable Changelog
Apc. enabled 1 PHP_INI_ALL
Apc. shm_segments 1 PHP_INI_SYSTEM
Apc. shm_size 30 PHP_INI_SYSTEM
Apc. optimization 0 PHP_INI_ALL
Apc. num_files_hint 1000 PHP_INI_SYSTEM
Apc. ttl 0 PHP_INI_SYSTEM
Apc. gc_ttl 3600 PHP_INI_SYSTEM
Apc. cache_by_default On PHP_INI_SYSTEM
Apc. filters "" PHP_INI_SYSTEM
Apc. mmap_file_mask "" PHP_INI_SYSTEM
Apc. slam_defense 0 PHP_INI_SYSTEM
Apc. file_update_protection 2 PHP_INI_SYSTEM
Apc. enable_cli 0 PHP_INI_SYSTEM> APC 3.0.6

Now that the configuration is complete, write the program.

XML/HTML code

The Code is as follows: Copy code

<! -The following is an upload form.->
<Form enctype = "multipart/form-data" id = "upload_form" action = "" method = "POST">
<Input type = "hidden" name = "APC_UPLOAD_PROGRESS" id = "progress_key" value = "upid"/>
Video title: <input type = "text" id = "subject" name = "subject"/>
Video Description: <input type = "text" id = "content" name = "content"/>
Video TAG (separated by commas) <input type = "text" id = "tag" name = "tags"/>
<Input type = "file" id = "upfile" name = "upfile"/>
<Input type = "submit" id = "filesubmit" value = "Upload" onclick = "startProgress ('upid '); return true;"/>
<! -Note: The parameters in startProgress ('upid ') are the unique upload parameters you have allocated from php.->
</Form>
<! -The upload progress bar is as follows->
<Div id = "upstatus" style = "width: 500px; height: 30px; border: 1px solid # ffffde; color: #796140;">
</Div
<Div id = "progressouter" style = "width: 500px; height: 20px; border: 3px solid # de7e00; display: none;">
<Div id = "progressinner" style = "position: relative; height: 20px; color: #796140; background-color: # f6d095; width: 0%;"> </div>
</Div>

The most important thing is the hidden domain of APC_UPLOAD_PROGRESS. With this script, you can access the status of the currently uploaded file. Just add a div that displays the upload status.

The following is the Ajax processing script. I use the Jquery framework to transmit messages in json format.

JavaScript code

The Code is as follows: Copy code


Function getProgress (upid ){
Var url = "<{$ siteurl}> epadmin/upprocess ";
$. GetJSON (
Url,
{Progress_key: upid },
Function (json ){
$ ("# Progressinner"). width (json. per + "% ");
$ ("# Upstatus" 2.16.html ('file size: '+ json. total + 'kb' + 'uploaded:' + json. current + 'kb ');
If (json. per <100 ){
SetTimeout (function (){
GetProgress (upid );
}, 10 );
} Else {
$ ("# Upstatus" ).html ("the video has been uploaded. processing data. Please wait ...... ");
}
}
)
}
Function startProgress (upid ){
$ ("# Progressouter" ).css ({display: "block "});
SetTimeout (function (){
GetProgress (upid );
},100 );
}

Again

The PHP code in the upload status is read. As for the process of uploading files, you can write them by yourself.

 

The Code is as follows: Copy code
// File upload operation function, which can be compiled as needed
Function upflvAction ()
{
If ($ _ SERVER ['request _ method'] = 'post '){
$ Subject = trim ($ this-> f-> filter ($ this-> _ request-> getPost ('subobject ')));
$ Content = trim ($ this-> f-> filter ($ this-> _ request-> getPost ('content ')));
Zend_Loader: loadClass ('m M _ flvop ');
$ Flv = new Custom_FlvOp;
$ Flv-> uploadFlv ('upfile', $ subject, $ content );
}
}
// This is the function used to read the upload status ~~
Function upprocessAction ()
{
If (isset ($ _ GET ['Progress _ key']) {
$ Status = apc_fetch ('upload _ '. $ _ GET ['ss SS _ key']);
$ Json = array (
& Apos; per & apos; = & apos; $ status ['current']/$ status ['Total'] * 100,
'Total' => round ($ status ['Total']/1024 ),
'Current' => round ($ status ['current']/1024 ),
);
Require_once ("Zend/Json. php ");
Echo Zend_Json: encode ($ json );
}
}

Details about apc Configuration

Apc. enabled Boolean
Apc. enabled can be set to 0 to disable APC. This is mainly useful. When APC is statically compiled into PHP, It is disabled because there is no other method (when it is compiled into DSO, php can be used. the extension line in ini is commented out ).

Apc. shm_segments integer
The number of shared memory blocks allocated to the compilation cache. If APC runs out of shared memory and you have set apc. shm_size to the maximum value allowed by the system, you can increase the value of this parameter.

Apc. shm_size integer
The size of each shared memory block is measured in MB. By default, the size of shared memory blocks in some systems (including most BSD Variant Systems) is very low.

Apc. optimization integer
Optimization level. If it is set to 0, optimization is disabled. The higher the value, the more powerful the optimization is. We are looking forward to improvement in speed. This is experimental.

Apc. num_files_hint integer
The number of source files that are included and requested on your Web server. If you are not sure, set it to 0 or omitted. This setting may be mainly used for sites with thousands of source files.

Apc. ttl integer
When a cache entry is required by another entry in the cache area, we need to consider the number of seconds that the cache entry can be idle in the cache area. Setting this parameter to 0 means that your cache may be filled with fresh entries, and new entries cannot be cached.

Apc. gc_ttl integer
The number of seconds that a cache entry will survive in the garbage collection list. This value provides error protection when executing a cache source file while the server process is dead. If the source file is modified, the cache entries allocated to the old version of the memory will not be recycled until the TTL value set by this parameter reaches. Setting 0 is to disable this feature.

Apc. cache_by_default Boolean Type
The default value is On, but it can be set to Off and used with apc. filters starting with the plus sign. The file is cached only when the filter is matched.

Apc. filters string
A list of POSIX extension Regular Expressions separated by commas. If any Mode matches the source file name, the file will not be cached. Note that the file name used for matching is the file name passed to include/require, rather than the absolute path. If the first character of the regular expression is +, the expression means that any file that matches the expression will be cached. If the first character is-, no matching will be cached. -Is the default value, so it can be omitted.

Apc. mmap_file_mask string


Apc. slam_defense integer
On a very busy server, whether you start the service or modify files, you will cause a multi-process to try to cache the competition for the same file at the same time. This option sets the percentage of files that the process skips and tries to cache. Or you can think of this as the probability that a separate process will skip the cache. For example, setting apc. slam_defense to 75 means that the process has a 75% chance of not caching files that are not cached. Therefore, the higher the setting, the more likely the cache collision rate can be reduced. If it is set to 0, this feature is disabled.

Apc. file_update_protection integer
When you modify a file on a running server, you should perform atomic operations. That is, first write a temporary file, and then rename the file (mv) to its final location. Many text editors, cp, tar, and other similar programs do not perform this operation. This means that you have the opportunity to access (cache) the file while it is still being written. The apc. file_update_protection setting causes the cache to mark the delay of new files. The default value is 2, which means that the file will not be cached if the modification time of the file is less than 2 seconds from the Access time. Unfortunately, users who access half of the files they write will see a strange situation, but at least this is not continuous. If you are sure that you often use atomic operations to update your files, you can disable this protection by setting this parameter to 0. If your system is full of io operations and it takes more than 2 seconds to update the program, you may need to increase the value.

Apc. enable-cli integer
Mostly for testing and debugging. Enable the APC function for PHP in the CLI version. In general, you will not expect to create, port, or discard the APC cache for each CLI request, but it is easy to enable APC for the CLI version in various test cases.


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.