PHP uses APC module to implement the file upload progress bar method, _php tutorial

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

PHP uses the APC module to implement the file upload progress bar method,


This paper introduces the method of using APC module to implement file upload progress bar in PHP. Share to everyone for your reference. The specific analysis is as follows:

The previous version of php5.2 is not available for APC modules because there is no previous APC module, and if you want to implement the upload progress bar using the APC module we must be php5.2 or later.

Starting from 5.2 APC joined a apc_upload_progress, which solves the problem of the progress bar that has been bothering you for a long time. And it put the original upload when the temporary files all cached to the memory to change when the temporary file reached the set value automatically saved to the hard disk, effectively improve the memory utilization.

It works by giving each upload a unique ID at upload time, and when the PHP script receives an upload file, the interpreter automatically checks the hidden field named Apc_upload_progress in the $_post array, which becomes the cache variable, storing information about the upload, This allows the script to access the uploaded file's status information via the uploaded ID.

APC is the abbreviation for alternative PHP cache and is a free, publicly available, optimized code cache for PHP. It is used to provide 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 the 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 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, it's all set, and now it's time to write the program.

The xml/html code is as follows :
Copy the Code code as follows:






The main thing is that Apc_upload_progress's hidden domain, with its script to access the current upload file status, plus a display upload status of the div is good.

Here is the script that deals with Ajax, I use the jquery framework, JSON to pass the message.

The JavaScript code is as follows:
Copy CodeThe code is 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 the PHP code to read the upload status, as for the processing of the upload file can be written in accordance with normal, the code is as follows:
Copy CodeThe code is as follows://Upload file operation function, can be written 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 upload status ~ ~
function Upprocessaction ()
{
if (Isset ($_get[' Progress_key ')) {
$status = Apc_fetch (' upload_ '. $_get[' Progress_key ');
$json = Array (
' per ' = = $status [' current ']/$status [' Total ']*100,
' Total ' =>round ($status [' Total ']/1024),
' Current ' =>round ($status [']/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 to zero disable APC, which is mainly useful, when APC is statically compiled into PHP, because there is no other way to disable it, when compiled to the DSO, the php.ini in the extension line can be commented out.

Apc.shm_segments Integral type

To allocate the number of shared memory blocks to the compilation cache, if the APC is running out of shared memory and you have set Apc.shm_size to the maximum allowable value for 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, the size of shared memory blocks for some systems (including most BSD variant systems) is very low.

Apc.optimization Integral type

Optimization level. Set to 0 to disable optimization, the higher the value the more powerful the optimization is used. Expect to have moderate speed improvements. This is still an experimental nature.

Apc.num_files_hint Integral type

A hint to 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 that have 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 filled with stale entries and that new entries cannot be cached.

Apc.gc_ttl Integral type

The number of seconds that the cache 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 set by this parameter is reached. Setting to 0 disables this feature.

Apc.cache_by_default Boolean type

The default is on, but can be set to off and used with the apc.filters with the plus sign, and the file is cached only when matching the filter.

Apc.filters string

A comma-delimited list of POSIX extended regular expressions. If any pattern matches the source file name, 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 +, then 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 a very busy server, whether you start the service or modify the file, you will cause a multi-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 single process that skips the cache's chances. 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 it will be to reduce the probability of collisions with the cache. Set to 0 to disable this feature.

Apc.file_update_protection Integral type

When you modify a file on a running server, you should perform atomic operations. That is, write a temporary file, and then rename (MV) the file to its final location when you finish writing it. Many text editors, Cp,tar, and other similar programs do not. This means there is a chance to access and (cache) the file when the file is still being written. The Apc.file_update_protection setting allows the cache to mark the delay of a new file. The default value is 2, which means that files will not be cached if they are found to be less than 2 seconds away from the time the file was modified. The unfortunate user who accesses half of the files will see the bizarre situation, but at least this is not a continuous situation. If you are sure that you often use atomic operations to update your files, you can turn this protection off by setting this parameter to 0. If your system is full of IO operations and causes the updater to take more than 2 seconds, you may need to increase this value.

Apc.enable-cli Integral type

Mostly for testing and debugging, starting the APC feature for CLI versions of PHP, in general, you will not expect to create, migrate and discard APC caches for each CLI request, but for various test cases it is easy to open APC for the CLI version.

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/947913.html www.bkjia.com true http://www.bkjia.com/PHPjc/947913.html techarticle PHP using APC module to implement the file upload progress bar method, this paper describes the use of APC module PHP implementation of the file upload progress bar method. Share to everyone for your reference. A specific analysis such as ...

  • 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.