This article mainly introduces how PHP uses the APC module to implement the File Upload progress bar, analyzes the specific usage of the APC module, and provides related configuration instructions. For more information, see
This article mainly introduces how PHP uses the APC module to implement the File Upload progress bar, analyzes the specific usage of the APC module, and provides related configuration instructions. For more information, see
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.
The following code configures parameters of the APC module:
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