This article is an example of PHP using APC module to achieve file upload progress bar method. Share to everyone for your reference. The specific analysis is as follows:
Prior to the php5.2 version is not able to use the APC module, because there is no prior to this APC module, if you want to use the APC module to implement upload progress bar we must be php5.2 or later version.
Since 5.2, APC joined a apc_upload_progress, which solves the problem of the progress bar that has plagued us for a long time. And it is the original upload when the temporary files are all cached to memory, when the temporary file reached the set value automatically saved to the hard disk, effectively improve the memory utilization situation.
The principle of its function is to upload each upload a unique ID, when the PHP script received an upload file, the interpreter will automatically check the $_post array named apc_upload_progress hidden field, it will become a cache variable, storing information about the upload, This allows the script to access the status information of the uploaded file through the uploaded ID.
APC is the abbreviation of alternative PHP cache and is a free public optimized code cache for PHP. It is used to provide a free, open and robust architecture to cache and optimize PHP's intermediate code.
the parameters of the APC module are configured with the following code:
Copy Code code as follows:
Name Default Changeable Changelog
Apc.enabled 1 Php_ini_all
Apc.shm_segments 1 Php_ini_system
Apc.shm_size 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
All right, we're ready to start writing the program now.
The xml/html code is as follows :
Copy Code code as follows:
<!– below is the 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 (comma-separated) <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 only upload parameters you assign from PHP –>
</form>
<!– below for upload progress bar –>
<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 is that apc_upload_progress hidden domain, with its script to access the current status of uploaded files, plus a display of the status of the Div.
Here's a script that handles Ajax, I use the jquery framework, and JSON delivers the message.
The JavaScript code is as follows :
Copy Code code as follows:
function Getprogress (upid) {
var url = "<{$siteurl}>epadmin/upprocess";
$.getjson (
Url
{Progress_key:upid},
function (JSON) {
$ ("#progressinner"). Width (json.per+ "%");
$ ("#upstatus"). html (' File size: ' +json.total+ ' kb ' + ' uploaded: ' +json.current+ ' KB ');
if (Json.per < 100) {
settimeout (function () {
Getprogress (UPID);
}, 10);
}else{
$ ("#upstatus"). HTML ("Video upload complete, processing data, please later ...");
}
}
)
}
function Startprogress (upid) {
$ ("#progressouter"). CSS ({display: "block"});
settimeout (function () {
Getprogress (UPID);
}, 100);
}
The following is read the upload state of the PHP code, as for the upload file processing can be written in accordance with their usual, the code is as follows:
Copy Code code as follows:
Upload file operation function, you can write according to your own needs
function Upflvaction ()
{
if ($_server[' Request_method ']== ' POST ') {
$subject = Trim ($this->f->filter ($this->_request->getpost (' subject '));
$content = Trim ($this->f->filter ($this->_request->getpost (' content '));
Zend_loader::loadclass (' Custom_flvop ');
$flv = new Custom_flvop;
$flv->uploadflv (' Upfile ', $subject, $content);
}
}
This is the function to read the status of the upload ~ ~
function Upprocessaction ()
{
if (Isset ($_get[' Progress_key ')) {
$status = Apc_fetch (' upload_ '. $_get[' Progress_key '));
$json = Array (
' Per ' => $status [the ' current ']/$status [' Total ']*100,
' Total ' =>round ($status [' Total ']/1024],
' =>round ' ($status [' current ']/1024],
);
Require_once ("zend/json.php");
echo Zend_json::encode ($json);
}
}
Some detailed information about APC configuration:
apc.enabled Boolean type
Apc.enabled can be set as 0来 disable APC, this is mainly useful, when the APC is statically compiled into PHP, because there is no other way to disable it, when compiling to the DSO, you can php.ini the extension line comment out.
Apc.shm_segments Integral type
The number of shared memory blocks allocated to the compilation cache, if the APC runs out of shared memory, and you have set the apc.shm_size to the maximum allowed by the system, you can try to increase the value of this parameter.
Apc.shm_size Integral type
The size of each shared memory block is in megabytes. By default, some systems (including most BSD variant systems) have a low size limit for shared memory blocks.
Apc.optimization Integral type
Optimization level. Set to 0 disables optimization, and the higher the value, the stronger the optimization. Look forward to a modest improvement in speed. This is still experimental in nature.
Apc.num_files_hint Integral type
Tips on the number of different source files that are included and requested on your Web server. If you are unsure, set to 0 or omit; This setting may be used primarily for sites with thousands of source files.
Apc.ttl Integral type
When a cache entry is required by another entry in the buffer location, we need to consider the number of seconds that the cache entry is allowed to be idle in the buffer location. Setting this parameter to 0 means that your cache may be full of stale entries and that new entries cannot be cached.
Apc.gc_ttl Integral type
The number of seconds the cached entry survives in the garbage collection list. This value provides an error protection in the event that a cache source file is executed while the server process is dead. If the source file is modified, the memory allocated to the old version of the cache entry will not be recycled until the TTL value is set to this parameter. Setting to 0 prohibits this feature.
Apc.cache_by_default Boolean type
The default is on, but it can be set to off and used in conjunction with a apc.filters that starts with a plus sign, and the file is cached only when the filter is matched.
Apc.filters string
A comma-separated list of POSIX-extended regular expressions. If any pattern matches the source filename, the file will not be cached. Note the filename used to match is the file name passed to Include/require, not 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, and if the first character is-then no match will be cached. -Is the default value, so it can be omitted.
Apc.mmap_file_mask string
Apc.slam_defense Integral type
On very busy servers, whether you start a service or modify a file, you can cause a multiple process to try to cache the same file at the same time. This option sets the percentage of the process skipping attempts to cache a file that is not cached. Or you can think of this as a chance for a separate process to skip caching. 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 it can reduce the collision probability of the cache. This attribute is disabled when set to 0.
Apc.file_update_protection Integral type
When you modify a file on a running server, you should perform an atomic operation. That is, write a temporary file first, and then rename (MV) the file to its final position when it is finished. Many text editors, Cp,tar, and other similar programs do not operate this way. This means that there is a chance to access and (cache) the file when the file is still being written. The Apc.file_update_protection setting causes the cache to mark the delay of the new file. The default value is 2, which means that if the file is found to be modified for less than 2 seconds, the file will not be cached. Unfortunate users who visit half of the files will see bizarre situations, but at least this is not sustainable. If you are sure that you often use atomic operations to update your files, you can turn off this protection by setting this parameter to 0. If your system is full of IO operations and causes the update to take more than 2 seconds, you may need to increase the value.
Apc.enable-cli Integral type
Mostly for testing and debugging, starting APC for CLI version of PHP, in general, you will not think of creating, porting and discarding the APC cache for each CLI request, but for a variety of test scenarios it is easy to turn on the APC for the CLI version.
I hope this article will help you with your PHP program design.