<? 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>" ;}}} ?>