PHP uses the APC module to implement the file upload progress bar. _ PHP Tutorial

Source: Internet
Author: User
Tags apc configuration apc module file upload progress bar
PHP uses the APC module to implement the file upload progress bar ,. PHP uses the APC module to implement the file upload progress bar. This article describes how PHP uses the APC module to implement the file upload progress bar. Share it with you for your reference. For details, PHP uses the APC module to implement the file upload progress bar,

This example describes how PHP uses the APC module to implement a file Upload progress bar. Share it with you for your reference. The specific analysis is as follows:

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 configurationThe code is as follows:

The code is as follows:

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.

The XML/HTML code is as follows::

The code is as follows:








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 p to display the upload status.

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

The JavaScript code is as follows::

The code is as follows:

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


The following is the PHP code for reading the upload status. As for the process of uploading files, you can write the code as usual. the code is as follows:

The code is as follows:

// 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 );
}
}


Some 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, because there is no other way to disable it. when it is compiled into DSO, php. comment out the extension line in ini.

Apc. shm_segments integer

Allocate the number of shared memory blocks to the compilation cache. if APC uses up the shared memory, you have set apc. when shm_size is the maximum value allowed by the system, you can try to 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

Most of them enable the APC function for PHP in the CLI version for testing and debugging. In general, you will not think of creating, porting, or discarding the APC cache for every CLI request, however, for various tests, it is easy to enable APC for the CLI version.

I hope this article will help you with php programming.

Example: PHP uses the APC module to implement a file Upload progress bar. Share it with you for your reference. The specific analysis is as follows...

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.