Simple PHP multi-file upload source code

Source: Internet
Author: User
<?  PHP session_start (); header ("Content-Type: text/html; charset = UTF-8"); include_once 'upload. class. PHP '; upload: setverifybysession (); if ($ _ post ["SBT"]) {$ upload = new upload ("pic ", $ _ post ["verify"]); $ picname = $ upload-> uploadfile ();}  ?>  <  Html  >   <  Meta  HTTP-equiv  = "Content-Type"  Content  = "Text/html; charsets = UTF-8"  >  <  Head  >   <  Title  > Multi-File Upload </  Title  >   </  Head  >   <  Body  >   <  A Href  = "#"  Onclick  = "Add ()"  > Add Image Upload window </  A  >  <  Form  Action  = ""  Method  = "Post"  Enctype  = "Multipart/form-Data"  Onsubmit = "Return BTN ();"  >  <  Div  ID  = "Inputarea"  >      <  Input  Type  = "File"  Name  = "PIC []"   />  </  Div  > <  Input  Type  = "Submit"  Name  = "SBT"  Value  = "Upload"   />  <  Input  Type  = "Hidden"  Name  = "Verify"  Value  = '<? PHP Echo $ _ session ["verify"]?  >  '/>  </  Form  >  <  Script  Type  = "Text/JavaScript"  >      Function  BTN (){  VaR  Flag =   False  ;  VaR  Objs  =  Document. getelementsbyname (  "  PIC []  "  ); For  (  VaR  I  =  0  ; I  <  Objs. length; I  ++  ){  VaR OBJ  =  Objs [I];  If  (Obj. Value  ! =   ""  ) {Flag  =   True  ;}} Return  Flag ;}  Function  Add (){  VaR  Input  =  Document. getelementbyid (  "  Inputarea  " ); Input. innerhtml  + =   "  <Br/> <input type = \ "file \" name = \ "PIC [] \"/>  "  ;}  </  Script  >  </  Body  >   </ Html  >  

Upload. Class. php

<? PHP  /*  ** Image Upload class  */  Class  Upload {  Private   $ Verify ; //  Used to refresh the comparison parameters that cause repeated uploads      Private   $ Dir ; // Upload directory      Private   $ Img ; //  Uploaded Image Information          Public   Function _ Construct ( $ Inputname , $ Verify  ){  Session_start  ();  $ This -> IMG = $ _ Files [ $ Inputname ];  $ This -> Verify = $ Verify  ;  $ This -> Dir = Dirname ( _ File __ ). "\ Uploads" ;}  /*  ** Set to prevent repeated refresh of the submitted comparison parameter verify  */      Public   Static  Function  Setverifybysession (){  If (! Isset ( $ _ Session ["Verify" ]) {  $ _ Session ["Verify"] = Time  ();}}  /*  ** Main Image Upload Logic  */      Public   Function Uploadfile () {self :: Repeatsubmit (); self :: Directoryexist ();  $ I = 0 ;  Foreach ( $ This -> IMG ["tmp_name"] As   $ TMP  ){  $ Filename = Explode (".", $ This -> IMG ["name"] [$ I ++ ]);  $ Filename = Self: setname ( $ Filename  );  If ( $ TMP  ){  $ Savepath = $ This -> Dir ."\\". $ Filename  ;  $ Stats = Move_uploaded_file ( $ TMP , $ Savepath  ); Self : Showpic ( $ Stats , $ Filename  );}}}  /*  ** Judge whether repeated submission is performed  */      Private   Function  Repeatsubmit (){  If ($ This -> Verify! = $ _ Session ["Verify" ]) {  Echo "<SCRIPT> alert ('do not submit it again! '); Window. Location. href ='./'</SCRIPT>" ;  Exit  ();}  Else  {  Unset ( $ _ Session ["Verify" ]) ;}}  /* ** Determine whether the upload directory exists  */      Private   Function  Directoryexist (){  If (! File_exists ( $ This -> Dir  )){  Mkdir ( $ This -> Dir  );}} /*  ** Rename the uploaded file  */      Private   Function Setname ( $ F  ){  $ Name = MD5 ( $ F [0]. Date ("Mdhis "). Rand (1, Time  ()));  Return  $ Name .".". $ F [1 ];}  /*  ** After the upload is successful, the uploaded image is displayed.  */      Private   Function Showpic ( $ Stats , $ Filename  ){  If ( $ Stats  ){  Echo " $ Filename '/> ". $ Filename . "<Br>" ;}}} ?>

 

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.