Php file Upload class complete instance

Source: Internet
Author: User
Tags php file upload
This article mainly introduces the php file Upload class, and analyzes the php file Upload type judgment, size computer restrictions, and other skills in the form of a complete example, for more information about php file uploading, see the example in this article. We will share this with you for your reference. The details are as follows:

/** $ File = new class_file ($ file_array, "flash/"); $ file-> set_allow_type (array ("jpg", "jpeg", "gif ")); $ file-> is_limit_size (); if (! $ File-> allow_file_size () {echo $ file-> error; exit;} if (! $ File-> allow_file_type () {echo $ file-> error; exit ();} else if (! $ File-> uploadfile () {echo $ file-> error; exit;} **/<? Php class class_file {private $ file_type; private $ file_size; private $ save_path; private $ file_path; private $ allow_type = array (); private $ allow_size; private $ file_name; private $ flag = false; private $ mime_type; private $ is_limit_size = false; public $ error; // Constructor function class_file ($ file_array, $ save_path) {$ this-> file_path = $ file_array ['tmp _ name']; $ this-> file_size = $ file_array ['size']; $ this-> file _ Type = $ file_array ['type']; $ this-> save_path = $ save_path;} // sets the allowed file type function set_allow_type ($ allow_type) {$ this-> allow_type = $ allow_type;} // sets the allowed file size function set_allow_size ($ allow_size) {$ this-> allow_size = $ allow_size ;} // File upload public function uploadfile () {if (! $ This-> allow_file_type () {$ this-> file_name ();} if (move_uploaded_file ($ this-> file_path, $ this-> save_path. $ this-> file_name) {return true;} else {$ this-> error = "file Upload failed"; return ;}} // Determine the file upload type function allow_file_type () {$ this-> file_name (); if (in_array ($ this-> mime_type, $ this-> allow_type )) {return true;} else {$ this-> error = "Upload type not allowed"; exit ();}} // Determine the file upload size function allow_file_size ($ size = 100) {if ($ th Is-> is_limit_size) {$ this-> set_allow_size ($ size); if ($ this-> allow_size >=$ this-> file_size) {return true ;} else {$ this-> error = "the file upload size limit is exceeded" ;}}// whether to restrict the file size function is_limit_size () {$ this-> is_limit_size = true ;} // file type and file name function file_name () {$ this-> mime_type = substr ($ this-> file_type, strpos ($ this-> file_type ,"/") + 1); if ($ this-> mime_type = "pjpeg") {$ this-> mime_type = "jpg ";} if ($ this-> mime_type = "x-ms -Wma ") {$ this-> mime_type =" wma ";} if ($ this-> mime_type =" x-ms-wmv ") {$ this-> mime_type = "wmv" ;}$ this-> file_name = date ("YmdHis "). ". $ this-> mime_type ";} function _ get_file_name () {return $ this-> file_name ;}}?>

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.