Foreword: Hi, I am not a tank. I am 54chen. I am entrusted by the motivation of the Brothers Society. I will pass a technical article for everyone to eat and watch while eating and to strengthen digestion.
With the development of the Internet, more and more technologies are beginning to focus on user experience. People-oriented is a long-term solution. So when uploading, everyone no longer meets a single “browse” button and has launched an upload progress bar — the function. As an interpreted language, PHP, how to do the detection of uploaded files, how to implement the upload progress bar with its behind-the-scenes principle, 54chen will be developed step by step in this article.
I. Implementation articles
In general, using PHP to implement the upload progress bar is as follows:
1. APC extension (the author is the founder of PHP, PHP has joined the APC extension after 5.2)
2.PECL extension module upload progress
Whether it is APC or upload progress, you need to compile the source code, because the original PHP function is impossible to read the contents of the temporary folder. Let’s look at how to use and the key code:
1 2 3 4 5 6 7 8 9 10 |
IF ( $ _SERVER [ 'REQUEST_METHOD' ] == 'the POST' ) { // upload request $ Status = apc_fetch ( 'upload_' . $ _POST [ 'APC_UPLOAD_PROGRESS' ] ) ; $ Status [ 'DONE' ] = . 1 ; echo json_encode ( $status ) ; // Output to the ajax call in the client page, please find the exit for the relevant documentation ; } elseif ( isset ( $_GET [ 'progress_key' ] )) { // read the upload progress $ Status = apc_fetch ( 'upload_' . $ _GET [ 'progress_key' ] ) ; echo json_encode ( $ Status ) ; Exit ; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 twenty one twenty two twenty three twenty four 25 26 |
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 ) ) { //Uploaded successfully } } } Elseif ( ! empty ( $_GET [ 'sessid' ] ) ) { header ( "Expires: Mon, 26 Jul 1997 05:00:00 GMT") ; Header ( "Last-Modified:" . Gmdate ( "D, D the MYH: I: S" ) . "GMT" ) ; header ( "the Cache-Control: NO-Store, NO-Cache, MUST-revalidate" ) ; header ( "the Cache-Control: POST-Check = 0, pre-Check = 0" , to false ) ; header ( "Pragma: NO-Cache" ) ; header ( "the Content-the 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, additionally processing logic } } |
II. Principles
Notice the red function in the previous article.
Download to uploadprogress1.0.1 for source code analysis and comment in the code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 twenty one twenty two twenty three twenty four 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
static void uploadprogress_file_php_get_info ( char * ID , the zval * the return_value ) { char S [ 1024 ] ; char * filename ; char * Template ; the FILE * F. ; TSRMLS_FETCH ( ) ; Template = INI_STR ( "uploadprogress.file.filename_template" ) ; // Read the set template if ( strcmp ( here)Template , "" ) == 0 ) { return ; } else { filename = uploadprogress_mk_filename ( id , template ) ; // If yes, create if ( ! filename ) return ; F = VCWD_FOPEN ( filename , "rb" ) ; if ( F ) { array_init ( return_value ) ; while (Fgets ( s , 1000 , F ) ) { // read the first address of the result data of a string *s from the stream; 1000-1: read the length of the data block at a time, the default value is 1k, ie 1024; F The file pointer char * k , * v , * e ; int index = 0 ; e = strchr ( s , '=' ) ; // find the first occurrence of the character = in the string s if ( ! e ) continue ; * e = 0 ; /* break the line into 2 parts */ v= e + 1 ; k = s ; /* trim spaces in front of the name/value */ while ( * k && * k <= 32 ) k ++; while ( * v && * v <= 32 ) v + +; /* trim spaces everywhere in the name */ for ( e = k ; * e ; e ++ ) if ( * e <= 32 ) { * e = 0 ; break ; } /* trim spaces only at the end of the value */ /* http://pecl.php.net/bugs/bug.php?id=14525 */ //for (e=v; *e; e++) if (*e <= 32) { *e = 0; break; } if (v != NULL) { // when the file has content for (index = strlen(v) ; index > 0; index--) { if ( v [ index ] > 32 ) break ; //cumulative v [ index ] = 0 ; } } add_assoc_string ( return_value , k, v , 1 ) ; } Fclose ( F ) ; } } If ( filename ) efree ( filename ) ; return ; } |
Also found in the source code:
1 2 3 4 5 6 |
PHP_MINIT_FUNCTION ( uploadprogress ) { REGISTER_INI_ENTRIES ( ) ; php_rfc1867_callback = uploadprogress_php_rfc1867_file ; return SUCCESS ; } |
Modified php_rfc1867_callback in MINIT to extract the key code of uploadprogress_php_rfc1867_file:
1 2 3 4 5 6 7 8 9 10 11 12 |
an upload_id = emalloc ( strlen ( * E_DATA -> value ) + . 1 ) ; strcpy ( an upload_id , * E_DATA -> value ) ; Progress -> an upload_id = an upload_id ; Progress -> time_last = Time ( NULL ) ; Progress -> speed_average = 0 ; progress -> speed_last = 0 ; progress -> bytes_uploaded = read_bytes ; progress -> files_uploaded = 0 ; progress -> est_sec = 0 ; progress -> identifier = uploadprogress_mk_filename ( upload_id , template ) ; // put the temporary file progress -> identifier_tmp in the specified template location = emalloc ( strlen ( progress -> identifier ) + 4) ; Sprintf ( Progress -> identifier_tmp , "% s.wr" , Progress -> identifier ) ; |
What is php_rfc1867_callback, you can see the analysis of another brother of the brother school http://www.laruence.com/2008/11/07/586.html
Summary
When detecting the size of a temporary file, APC and upload progress are the same methods, first record, then take the size percentage.
Leave a Reply
You must be logged in to post a comment.