Php+ajax asynchronous with progress bar upload file

Source: Internet
Author: User
Tags php file upload

Front-end Ingest file

<link rel= "stylesheet" href= "Https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" >< Script src= "http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js" ></script><script src= "http/ Malsup.github.com/jquery.form.js "></script>

Ajax progress bar Asynchronous processing

<script type= "Text/javascript" >$ (function  ()  {      $ ("# Myupload "). Ajaxform ({          datatype: ' json ',           beforesend:function () {                    $ (". Progress"). Show ();           },           uploadprogress:function (Event,position,total,percentcomplete) {                   var percentVal =  percentcomplete +  '% ';                   $ (". Progress-bar"). Width (percentcomplete +  '% ');                   $ (". Progress-bar"). HTML (percentval);                   $ (". Sr-only"). HTML ( percentcomplete +  '% ');          },           success:function (data) {                   $ (". Progress"). Hide ();                              if (data.error ==  "Empty_name") {                            alert ("File upload is illegal, upload failed!");                           exit ();                   };                   if (data.error ==  "large") {                            alert ("Image upload cannot be greater than 2M, upload failed!");                            exit ();                   };    /*alert (Data.error);*/                   if ( data.error ==  "format") {&NBSP;&NBSP;&NBSP;&NBSP;&NBsp;                      alert ("Picture format error, upload failed");                           //alert ( Data.type);                           exit ();                   };                     //alert ("Upload succeeded!");                   // Files.html ("<b>" +data.name+ "(" +data.size+ "K) </b> <span class= ' delimg '  rel= '" + data.pic+ "' > Delete </span>");                   $ (". Files"). HTML ( "FileName: " +data.name+ "<span class= ' delimg '  rel= '" +data.pic+ "' >   del    </span> size: "+data.size);                   var img =  "http://www.sandleft.com/test/input/upload/ Files/"+data.pic;                   $ (". showimg"). HTML ("

Front-end upload HTML

 <div class= "Uk-container uk-container-center" >                 <div class= "Pk-system-messages" ></div> 

PHP File Upload class

<?phpclass upload{protected  $file _path =  "files";  //Current files Storage folder #protected $ file_size = 1024000;protected  $file _size = 5120000; //5m  User uploads//detects if the file is empty  public function check_file ($get _file)  {   if  (Empty ($get _file)     {      $type  =  "Check_file";               $arr  = array (' error ' = ' empty_name ', ' type ' = + $type);              echo json_encode ($arr);              exit ();         }return true;}  //detection file type  public function check_type ($get _type)  {  if  (  $get _ type ==  ". docx"  )  | |   (  $get _type ==  ". Doc"  ) &NBsp {       # $types  =  $get _type;  }else{         $type  =  "Check_type";        $arr  =  array (' ERROR ' = ' format ', ' type ' = + $type);                echo json_encode ($arr);                exit ();      }   return  true; } //detection File Size  public function check_size ($get _file)  { if  (   $get _file !=  ""  )  {             if  (  $get _file >  $this->file_size )  {                     $arr  =  Array (' ERROR ' = ' large '); &NBsp;                   echo json_encode ($arr);                    exit ();             }    }else{return false;exit ();}   return true; }   //File Save  public function save_file ($file _ Type, $file _tmp_name)  {$rand  = rand (1000, 9999), $pics  = date ("Ymdhis")  .   $rand  .  $file _type, $path  =  $this->file_path. " /". $pics; $result  = move_uploaded_file ($file _tmp_name,  $path); if ($result) {return  $pics;} Else{return false;exit ();} #return   $pics;  } }

PHP File Upload processing

<?phpinclude ("upload.class.php"); $up _obj = new upload ();  $get _filename = $_ files[' mypic ' [' name ']; $get _filesize = $_files[' mypic ' [' size ']; $get _tmpfiles = $_files[' Mypic ' [' tmp_name ']; $get _filetype = strstr ($get _filename,  '); $check _result =  $up _obj->check_file ($get _filename), if ($check _result) {    // Check file type $result_type =  $up _obj->check_type ($get _filetype);//Check File size if ($result _type) {$result _size  =  $up _obj->check_size ($get _filesize), if ($result _size) {//File upload save $pics =  $up _obj->save _file ($get _filetype, $get _tmpfiles); $size  = round ($get _filesize/1024,2);                   $arr  = array (                   ' name '  = >  $get _filename,                  ' pic '  =>  $pics,                   ' Size ' => $ size,                  ' Error '  => 2         );  //check file upload status  if ($pics {         echo json_encode ($arr); /*  perform upload completion logic ....  */}}    }}

File Upload effect

650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M01/87/DD/wKioL1fjnX_CBuoLAAAXbHEtYSs945.png-wh_500x0-wm_3 -wmp_4-s_1992695992.png "title=" Qq20160922165658.png "alt=" Wkiol1fjnx_cbuolaaaxbhetyss945.png-wh_50 "/>

This article is from the "Linux Technology" blog, so be sure to keep this source http://linuxinfo.blog.51cto.com/412748/1855478

Php+ajax asynchronous with progress bar upload file

Related Article

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.