Php+apc+ajax implementation of Ajax_upload upload progress bar code _php Tips

Source: Internet
Author: User
Tags apc apc module

The example of this article describes the Ajax_upload upload progress bar code of PHP+APC+AJAX implementation. Share to everyone for your reference, specific as follows:

How to upload the progress bar how to achieve it? When we browse, choose to upload, will produce a temporary file, upload the temporary file, uploaded to the server, upload completed, this temporary file will be deleted. If we can read the size of this temporary file, we know how much upload progress, the PHP APC module can achieve this function.

First, install the APC module

Download Address: HTTP://PECL.PHP.NET/PACKAGE/APC

Tar zxvf apc-3.1.8.tgz
cd apc-3.1.8/
/usr/local/php/bin/phpize
./configure--with-php-config=/usr/ Local/php/bin/php-config make
&& make install

Ii. Modification of PHP.ini

Extension = apc.so
apc.rfc1867 = 1
apc.max_file_size = 200M
upload_max_filesize = 1000M
post_max_ Size = 1000M
max_execution_time =
Max_input_time =
Memory_limit = 128M

After you've modified it, start Apache or something, check it out.

[Root@blackghost php]# php-m
[php Modules]
APC
cgi-fcgi
CType
date
Dom
Eaccelerator .....


Third, upload_test.php

<?php $id = uniqid (rand (), true)?>  

What's the use of apc_upload_progress? It adds a tag to the uploaded file and can access it in other PHP programs using this tag. Provides support for the reading of APC.

Upload.js asynchronous upload of JS files:

 function Form_submit () {New Ajaxupload (' #upload ', {action: ' upload.php ', Name: ' File ', Data: {apc_upload_progress:$ ("#progress_key"). Val ()}, Autosubmit:true, onsubmit:function (file, extension) {$ (' #
Progressouter '). CSS (' Display ', ' block ');
Progress ();
}, Oncomplete:function (file, response) {$ ("#showInfo"). HTML (response);}); function Progress () {$.ajax ({type: "Get", url: "progress.php?progress_key=" +$ ("#progress_key"). Val (), DataType: " JSON ", Cache:false, Success:function (data) {if (data = = 0) {var precent = 0;} else {for (I-in data) {if (i = =)
") {var json_current = parseint (Data[i]);}
if (i = = "Total") {var json_total = parseint (Data[i]);}
var precent = parseint (json_current/json_total * 100);
$ ("#progressinner"). CSS ("width", precent+ "%");
$ ("#showNum"). html (precent+ "%");
$ ("#showInfo"). html ("OK");
} if (Precent <) {settimeout ("Progress ()", 100);}
}); }

There is a point to note, apc_upload_progress:$ ("#progress_key"). Val () Here, the key is apc_upload_progress if not, the APC cannot find the temporary file. Why should I use ajax_upload.js here, because jquery has its own Ajax, with parameters not uploaded files, that is, the content of type= ' file ', PHP end is not.

Four, upload.php upload files

<?php
if ($_server[' request_method '] = = ' POST ') {
if (empty ($_files["file" ["Tmp_name"])) {
echo No File ";
Die;
}
$tmp _name = $_files["File" ["Tmp_name"];
$name = dirname ($_server[' script_filename '). " /upload/". $_files[" File "[" name "];
Move_uploaded_file ($tmp _name, $name);
echo "<p>file uploaded.</p>";
>

If the file is larger, do not upload the HTTP way, too slow, and affect the stability of the site.

V. progress.php the progress of the file, to the AJAX call

<?php
if (isset ($_get[' Progress_key ')) {
$status = apc_fetch (' upload_ '. $_get[' Progress_key ']);
if ($status [' Total ']!=0 &&!empty ($status [' total ']) {
echo json_encode ($status);
} else {
echo 0;
}
}
? >

Look at the data generated by the Ajax asynchronous request.

PHP APC Ajax upload progress bar

Parameter description:

Total File Size
Current uploaded size
FileName upload filename
Name label Signature
Done Upload success for 1
Cancel_upload users to cancel upload, only upload completed when only
Rate upload speed, only upload completed when only
Start_time Start time

More about PHP files and Ajax Operations interested readers can view the site topics: "PHP file Operation Summary" and "Php+ajax Skills and Application Summary"

I hope this article will help you with the PHP program design.

Related Article

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.