This article mainly shares with you a simple PHP file Upload class for beginners, which has some reference value. interested friends can refer to the examples in this article to describe PHP multi-file Upload classes, share it with you for your reference. The details are as follows:
<? Phpclass Test_Upload {protected $ _ uploaded = array (); protected $ _ destination; protected $ _ max = 1024000; protected $ _ messages = array (); protected $ _ permited = array ('image/GIF', 'image/jpeg ', 'image/pjpeg', 'image/png '); protected $ _ renamed = false; /***** @ param mix $ path **/public function _ construct ($ path) {if (! Is_dir ($ path) |! Is_writable ($ path) {throw new Exception ("the file name cannot be written, or it is not a directory! ") ;}$ This-> _ destination = $ path; $ this-> _ uploaded =$ _ FILES;}/*** move FILE **/public function move () {$ filed = current ($ this-> _ uploaded); $ isOk = $ this-> checkError ($ filed ['name'], $ filed ['error']); // debug OK if ($ isOk) {$ sizeOk = $ this-> checkSize ($ filed ['name'], $ filed ['size']); $ typeOk = $ this-> checkType ($ filed ['name'], $ filed ['type']); if ($ sizeOk & $ typeOk) {$ success = move_uploaded_file ($ f Iled ['tmp _ name'], $ this-> _ destination. $ filed ['name']); if ($ success) {$ this-> _ messages [] = $ filed ['name']. "File uploaded successfully";} else {$ this-> _ messages [] = $ filed ['name']. "File upload failed" ;}}}/ *** query the content of the messages array **/public function getMessages () {return $ this-> _ messages ;} /*** check the size of the uploaded file * @ param mix $ string * @ param int $ size */public function checkSize ($ filename, $ size) {if ($ size = 0) {return false;} el Se if ($ size> $ this-> _ max) {$ this-> _ messages [] = "the file size exceeds the upload limit ". $ this-> getMaxsize (); return false;} else {return true ;}} /*** check the type of the uploaded file * @ param mix $ filename * @ param mix $ type */protected function checkType ($ filename, $ type) {if (! In_array ($ type, $ this-> _ permited) {$ this-> _ messages [] = "this file type is not an allowed Upload type"; return false ;} else {return true ;}/ *** get file size **/public function getMaxsize () {return number_format ($ this-> _ max/1024, 1 ). 'KB';}/*** check Upload error * @ param mix $ filename * @ param int $ error **/public function checkError ($ filename, $ error) {switch ($ error) {case 0: return true; case 1: case 2: $ this-> _ messages [] = "The file is too large! "; Return true; case 3: $ this-> _ messages [] =" file upload error! "; Return false; case 4: $ this-> _ messages [] =" no file is selected! "; Return false; default: $ this-> _ messages [] =" system error! "; Return false ;}}?>
I hope this article will help you with php programming.