Php uses iframe to implement the Code for the refreshing file upload function

Source: Internet
Author: User

Copy codeThe Code is as follows:
<Html>
<Head>
<Title> resumable upload of files </title>
<Meta Content-type = "text/html" charset = "UTF-8"/>
<Script type = "text/Webpage effects">
Function startUpload (){
Document. getElementById ('processing'). innerHTML = 'loding ...';
Return true;
}
Function stopUpload (rel ){
Var msg;
Switch (rel ){
Case 0:
Msg = "uploaded ";
Break;
Case 1:
Msg = "the number of uploaded files exceeds the limit ";
Break;
Case 2:
Msg = "only image files can be uploaded ";
Break;
Default:
Msg = "failed to upload the file ";
}
Document. getElementById ('processing'). innerHTML = msg;
}
</Script>
</Head>
<Body>
<Div style = "text-align: center">
<Div id = "processing"> </div>
<Form action = "upload. php tutorial" method = "post" enctype = "multipart/form-data" target = "form-target" onsubmit = "startUpload ();">
<Input type = "hidden" name = "MAX_FILE_SIZE" value = "1000000"/>
<Input type = "file" name = "myfile"/>
<Input type = "submit" name = "sub" value = "upload"/>
</Form>
<Iframe style = "width: 0; height: 0; border: 0;" name = "form-target"> </iframe>
</Div>
</Body>
</Html>
Php code
<? Php
Sleep (2 );
$ FileTypes = array ('jpg ', 'png', 'gif', 'bmp ');
$ Result = null;
$ UploadDir = './upfiles ';
$ MaxSize = 1 * pow (2, 20 );
If ($ _ SERVER ['request _ method'] = 'post' & isset ($ _ POST ['sub ']) {
$ Myfile = $ _ FILES ['myfile'];
$ MyfileType = substr ($ myfile ['name'], strrpos ($ myfile ['name'], ".") + 1 );
If ($ myfile ['SIZE']> $ maxSize ){
$ Result = 1;
} Else if (! In_array ($ myfileType, $ fileTypes )){
$ Result = 2;
} Elseif (is_uploaded_file ($ myfile ['tmp _ name']) {
$ ToFile = $ uploadDir. '/'. $ myfile ['name'];
If (@ move_uploaded_file ($ myfile ['tmp _ name'], $ toFile )){
$ Result = 0;
} Else {
$ Result =-1;
}
} Else {
$ Result = 1;
}
}
?>
<Script type = "text/javascript">
Window. top. window. stopUpload (<? Php echo $ result;?> );
</Script>

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.