PHP File Upload progress bar implementation Program _php Tutorial

Source: Internet
Author: User
Tags apc file upload progress bar php file upload
In PHP to implement the upload progress bar There are many ways, such as Ajax is now the mainstream or using an IFRAME to implement, now we introduce PHP APC and uploadprogress implementation of the file upload progress bar effect.

There are two ways I know now, one is to use the APC extension (HTTP://PECL.PHP.NET/PACKAGE/APC) written by the founder of Php Rasmus Lerdorf, and the other is to use the PECL extension module Uploadprogress implementation (http://pecl.php.net/package/uploadprogress) I'll give you two examples to implement separately for reference, more flexible applications to modify according to their own needs.


APC Implementation method:

Install APC, refer to the Official document installation, you can use the Pecl module installation method quick and simple, here does not explain
Configure the php.ini, set the parameter apc.rfc1867=1, enable APC to support the upload progress bar function, in the APC source code document contains the description
code example:

copy code
if ($_server[' request_method '] = = ' POST ') {// Upload request
$status = Apc_fetch (' upload_ '. $_post[' apc_upload_progress ');
$status [' done '] = 1;
Echo Json_encode ($ status); Output to the user-side page Ajax calls, related documents please look for
exit;
} elseif (Isset ($_get[' Progress_key ')) {//Read upload progress
$status = Apc_fetch (' Upload_ '. $_get[' Progress_key ');
Echo Json_encode ($status);
Exit;
} else {
//other code, such as upload form, etc.
}

Uploadprogress Module Implementation method:
Install the module using the PECL module installation method
PHP.ini inside set uploadprogress.file.filename_template = "/tmp/upd_%s.txt"

code example:

The code is as follows Copy Code

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, 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 {
Show Upload Form
}


Integration

Now all that's left is to hook everything together. You can do this through the progress.php page.


Listing 5. The final progress.php page

The code is as follows Copy Code

$id = Uniqid ("");
?>

Upload Example




"width:500px; height:20px; BORDER:6PX solid red; Display:none; " >
"Position:relative; height:20px; Background-color:purple; width:0%; ">



Starting at the bottom, we've added an IFRAME that embeds the upload.php script in Listing 1, giving it the unique ID generated at the top of the page.

Now, do you still remember the Submit button in the form?

The code is as follows Copy Code

Type= "Submit" value= "upload!" />


The button will do two tasks. Submit the form like a normal submit button, but before you do that, it will call the startprogress () script in the main window. The startprogress () script will tell the progress bar to show itself--no display properties at the beginning, then tell the browser to wait a second before executing the getprogress () script.

Now, the getprogress () script will make things interesting. Remember before I said I would need to use Ajax or some similar method to check the progress of a file? Yes, in this case, the form will take a shortcut to invoke the Gdownloadurl () function from the Google Maps API Library (note that the form will be imported into the library at the top of the page.) You will need to get your own key to access this library, but it is available free of charge from Google.

This function downloads the contents of the URL-in this case, the getprogress.php script-and executes the anonymous function defined in it. The first parameter that the function accepts is the data returned from the URL, in this case a percentage, so that the progress bar can be updated with it. Finally, if the file has not yet finished downloading, tell the browser to retry every one-tenth seconds (in practice, these calls may not be executed so quickly, but the browser will do its best to do so).

The end result is that the page allows the user to see the progress of the file being uploaded.


If the file is too large we can do the following:

PHP limit upload file size first:

Check the following line in the php.ini:

Upload_max_filesize = 8M

Post_max_size = 10M

Memory_limit = 20M

Change these values to what I said and see if there are any problems, and also confirm that the uploaded

The does not resemble the following line

This is also limited upload size.

PHP limit upload file size second:

If Apache 2 needs to be modified

/etc/httpd/conf.d/php.conf

Limitrequestbody 524288 will be 524 288 (=512x1024) to large, such as 5M (=5x1024x1024)

After the PHP limit upload file size, file upload will not appear above problems, upload does not respond, upload the reality of the page can not be realistic will be resolved!


http://www.bkjia.com/phpjc/444650.html www.bkjia.com True http://www.bkjia.com/phpjc/444650.html techarticle There are many ways to implement the upload progress bar in PHP, As Ajax is now the mainstream or using an IFRAME to achieve, now we introduce PHP APC and uploadprogress implementation file upload progress bar effect ...

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