There are two kinds of methods I know at the moment, one is implemented using the APC extension module written by the founder of PHP Rasmus Lerdorf (HTTP://PECL.PHP.NET/PACKAGE/APC), and the other is to use the PECL extension module Uploadprogress implementation (http://pecl.php.net/package/uploadprogress) Here are two examples of implementation, and more flexible applications are modified to suit their needs.
APC Implementation method:
Install APC, refer to official documentation installation, you can use the Pecl module installation method quickly and easily, here does not indicate
Configure php.ini, set parameters apc.rfc1867=1, enable APC support upload progress bar function, in the APC source code description document contains instructions
code example:
code is as follows |
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 AJAX calls in the client page, please find the relevant documents themselves 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 uploading a form } |
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, June June 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 failure, additional processing logic }
} else { Show Upload Form } |
Integration
Now all that's left is to hook up all the content together. You can do this by progress.php the page.
Listing 5. The final progress.php page
The code is as follows |
Copy Code |
<?php $id = Uniqid (""); ?> <body> <script src= "Http://maps.google.com/maps?file=api&v=2&key=<yourkeyhere>" Type= "Text/javascript" ></script> <script type= "Text/javascript" > function getprogress () { Gdownloadurl ("getprogress.php?progress_key=<?php Echo ($id)?>", function (Percent, Responsecode) { document.getElementById ("Progressinner"). Style.width = percent+ "%"; If (Percent < 100) { SetTimeout ("getprogress ()", 100); } }); } function startprogress () { document.getElementById ("Progressouter"). style.display= "Block"; SetTimeout ("getprogress ()", 1000); } </script> <iframe id= "Theframe" name= "Theframe" Src= "upload.php?id=<?php Echo ($id)?>" Style= "Border:none; height:100px; width:400px; "> </iframe> <br/><br/> <div id= "Progressouter" style= "width:500px; height:20px; BORDER:6PX solid red; Display:none; " > <div id= "Progressinner" style= "Position:relative; height:20px; Background-color:purple; width:0%; "> </div> </div> </body>
|
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 remember the Submit button in the form?
The code is as follows |
Copy Code |
<input onclick= "window.parent.startProgress (); return true; " Type= "Submit" value= "upload!" />
|
This button completes both tasks. Submit the form, just like the normal submit button, but before you do so, it will invoke the startprogress () script in the main window. The startprogress () script tells the progress bar to display itself-no display properties at first, and then tells the browser to wait a second before executing the getprogress () script.
Now, the getprogress () script will make things interesting. Remember before I said that you 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 and call the Gdownloadurl () function from the Google Maps API Library (note that the form will import the library at the top of the page). You will need to obtain your own key to access this library, but it is obtained 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 therein. The first parameter accepted by the function is the data returned from the URL, in this case as a percentage, so that it can be used to update the progress bar. Finally, if the file has not been downloaded yet, tell the browser to retry every one-tenth seconds (in reality, these calls may not be executed so quickly, but the browser will do what it can).
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:
View the following lines in the php.ini:
Upload_max_filesize = 8M
Post_max_size = 10M
Memory_limit = 20M
Change these values to what I say, see if there are any problems, and make sure that the uploaded <form> does not have the same line below.
<input type= "hidden" name= "max_file_size" value= "500000" > This is also limited to upload size.
PHP Limit upload file size second:
If Apache 2 needs to be modified
/etc/httpd/conf.d/php.conf
Limitrequestbody 524288 524288 (=512x1024), such as 5M (=5x1024x1024)
PHP limit upload file size, file upload will not appear as the problem, upload does not respond to upload the reality of the page can not be a reality will be resolved!