PHP uses apc to implement the upload progress bar

Source: Internet
Author: User
Tags apc

Php itself does not have the real-time upload progress bar function. If you want this function, we generally use ajax. However, php provides an apc, you can configure the upload progress bar with php.

It mainly targets applications on Windows.
1. The server must support apc extension. If this extension is not available, Baidu php_apc.dll should be used to download an extension that requires php.5.2 or later.
2. Configure apc and restart apache.

The Code is as follows: Copy code
Extension = php_apc.dll
Apc. rfc1867 = on
Apc. max_file_size = 1000 M
Upload_max_filesize = 1000 M
Post_max_size = 1000 M

 

Note: The size of parameters depends on the project's apc. max_file_size: Specifies the size of the files that can be uploaded by apc. apc is required. max_file_size <= upload_max_filesize and apc. max_file_size <= post_max_size. restart apache to support apc.
3. Use phpinfo () in the code to check whether the apc extension is installed.
4. The following is the implementation code:
Getprogress. php

The Code is as follows: Copy code

<? Php
Session_start ();
If (isset ($ _ GET ['Progress _ key']) {
$ Status = apc_fetch ('upload _ '. $ _ GET ['ss SS _ key']);
Echo ($ status ['current']/$ status ['Total']) * 100;
}
?>
Upload. php

PHP Code
<? Php
$ Id = $ _ GET ['id'];
?>
<Form enctype = "multipart/form-data" id = "upload_form" action = "target. php" method = "POST">
<Input type = "hidden" name = "APC_UPLOAD_PROGRESS"
Id = "progress_key" value = "<? Php echo $ id?> "/>
<Input type = "file" id = "test_file" name = "test_file"/> <br/>
<Input onclick = "window. parent. startProgress (); return true ;"
Type = "submit" value = "Upload"/>
</Form>

Target. php

The Code is as follows: Copy code
<? Php
Set_time_limit (600 );
If ($ _ SERVER ['request _ method'] = 'post '){
Move_uploaded_file ($ _ FILES ["test_file"] ["tmp_name"],
Dirname ($ _ SERVER ['script _ filename']). "/UploadTemp /". $ _ FILES ["test_file"] ["name"]); // The UploadTemp folder is located in the same directory as the script.
Echo "<p> upload successful </p> ";
}
?>

Index. php

The Code is as follows: Copy code

<? Php
$ Id = md5 (uniqid (rand (), true ));
?>
<Html>
<Head> <title> upload progress </title> <Body>
<Script src = "js/jquery-1.4.4.min.js" language = "javascript"> </script>


<Script language = "javascript">
Var proNum = 0;
Var loop = 0;
Var progressResult;
Function sendURL (){
$. Ajax ({
Type: 'get ',
Url: "getprogress. php? Progress_key = <? Php echo $ id;?> ",
Async: true,
Cache: false,
DataType: 'json ',
Data: "progress_key = <? Php echo $ id;?> ",
Success: function (e ){
ProgressResult = e;
ProNum = parseInt (progressResult );
Document. getElementById ("progressinner"). style. width = proNum + "% ";
Document. getElementById ("showNum"). innerHTML = proNum + "% ";
If (proNum <100 ){
SetTimeout ("getProgress ()", 100 );
}

}
});

}

Function getProgress (){
Loop ++;

SendURL ();
}
Var interval;
Function startProgress (){
Document. getElementById ("progressouter"). style. display = "block ";
SetTimeout ("getProgress ()", 100 );
}
</Script>
<Iframe id = "theframe" name = "theframe"
Src = "upload. php? Id = <? Php echo $ id;?> "
Style = "border: none; height: 100px; width: 400px;">
</Iframe>
<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>
<Div id = 'shownum'> </div> <br>
<Div id = 'shownum2'> </div>
</Body>
</Html>


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.