Simple PHP file Upload class for beginners

Source: Internet
Author: User
Tags php file upload
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.

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.