Code of two implementation methods for PHP File Upload progress bar

Source: Internet
Author: User
Tags apc file upload progress bar php file upload

Currently, I know two methods: one is to use the APC extension module written by Rasmus lerdorf, founder of PHP to implement (http://pecl.php.net/package/apc ), another way is to use the PECL extension module uploadprogress implementation (http://pecl.php.net/package/uploadprogress) I here are two examples of implementation respectively for reference, more flexible application according to their own needs to modify.

APC implementation method:

Install APC by referring to the official documentation. You can use the PECL module to install APC quickly and easily.
Configure PHP. ini and set the parameter APC. rfc1867 = 1 to enable APC to support the upload progress bar function, which is described in the APC source code instructions.
CodeExample:Copy codeThe Code is as follows: if ($ _ server ['request _ method'] = 'post') {// upload request
$ Status = apc_fetch ('upload _ '. $ _ post ['apc _ upload_progress']);
$ Status ['done'] = 1;
Echo json_encode ($ status); // Ajax call output to the user's page. Please find the relevant documentation
Exit;
} Elseif (isset ($ _ Get ['ss SS _ key']) {// read the upload progress
$ Status = apc_fetch ('upload _ '. $ _ Get ['ss SS _ key']);
Echo json_encode ($ status );
Exit;
} Else {
// Other code, such as uploading a form
}

Implementation of the uploadprogress module:

Use the PECL module Installation Method to install this module
Set uploadprogress. file. filename_template = "/tmp/upd_%s.txt" in PHP. ini"
Sample Code:Copy codeThe Code is as follows: if ($ _ server ['request _ method'] = 'post '){
If (is_uploaded_file ($ _ FILES ['upfile'] ['tmp _ name']) {
$ Upload_dir = 'your _ path /';
$ Ext = strrchr ($ _ FILES ['video'] ['name'], '.');
$ Sessid = $ _ post ['upload _ identifier '];
$ Tmpfile = $ upload_dir. $ sessid;
$ Sessfile = $ upload_dir. $ sessid. $ ext;
If (move_uploaded_file ($ _ FILES ['upfile'] ['tmp _ name'], $ tmpfile )){
// Upload successful
} Else {
// Upload Failed
} Else {
// Upload Error

} Elseif (! Empty ($ _ Get ['sessid ']) {
Header ("expires: Mon, 26 Jul 1997 05:00:00 GMT ");
Header ("last-modified:". gmdate ("D, D m y h: I: s"). "GMT ");
Header ("cache-control: No-store, no-cache, must-revalidate ");
Header ("cache-control: Post-check = 0, pre-check = 0", false );
Header ("Pragma: No-Cache ");
Header ("Content-Type: text/html; charset = UTF-8 ");

$ Unique_id = $ _ Get ['sessid'];
$ Uploadvalues = uploadprogress_get_info ($ unique_id );

If (is_array ($ uploadvalues )){
Echo json_encode ($ uploadvalues );
} Else {
// Read progress failed. Additional processing logic
}

} Else {
// Display the upload form
}

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.