Php file Upload class example _ php skills

Source: Internet
Author: User
This article mainly introduces the PHP file Upload class, and analyzes the PHP file Upload implementation code and related usage skills in combination with the instance form. For more information, see the example in this article. We will share this with you for your reference. The details are as follows:

The FileUpload. class. php file Upload class is demonstrated here. two constants are used, which can be defined in the website configuration file:

Define ('root _ path', dirname (_ FILE _); // define ('updir', '/uploads/') of the website ROOT directory; // upload the main directory

The code is as follows:

<? Php // upload File class FileUpload {private $ error; // error code private $ maxsize; // maximum form Value private $ type; // type private $ typeArr = array ('image/jpeg ', 'image/pjpeg', 'image/png ', 'image/x-png ', 'image/GIF'); // type collection private $ path; // directory path private $ today; // today's directory private $ name; // file name private $ tmp; // temporary file private $ linkpath; // link path private $ linktotay; // Current Directory (relative) // Constructor, initialize public function _ construct ($ _ file, $ _ maxsize ){ $ This-> error = $ _ FILES [$ _ file] ['error']; $ this-> maxsize =$ _ maxsize/1024; $ this-> type = $ _ FILES [$ _ file] ['type']; $ this-> path = ROOT_PATH.UPDIR; $ this-> linktotay = date ('ymmd '). '/'; $ this-> today = $ this-> path. $ this-> linktotay; $ this-> name = $ _ FILES [$ _ file] ['name']; $ this-> tmp = $ _ FILES [$ _ file] ['tmp _ name']; $ this-> checkError (); $ this-> checkType (); $ this-> checkPath (); $ this-> moveUpload ();} // return path Public function getPath () {$ _ path = $ _ SERVER ["SCRIPT_NAME"]; $ _ dir = dirname ($ _ path )); if ($ _ dir = '\') $ _ dir = '/'; $ this-> linkpath = $ _ dir. $ this-> linkpath; return $ this-> linkpath;} // move the file private function moveUpload () {if (is_uploaded_file ($ this-> tmp) {if (! Move_uploaded_file ($ this-> tmp, $ this-> setNewName () {Tool: alertBack ('warning: Upload failed! ') ;}} Else {Tool: alertBack (' warning: the temporary file does not exist! ') ;}} // Set the new file name private function setNewName () {$ _ nameArr = explode ('. ', $ this-> name); $ _ postfix = $ _ nameArr [count ($ _ nameArr)-1]; $ _ newname = date ('ymdhis '). mt_rand (100,1000 ). '. '. $ _ postfix; $ this-> linkpath = UPDIR. $ this-> linktotay. $ _ newname; return $ this-> today. $ _ newname;} // verify the private function checkPath () {if (! Is_dir ($ this-> path) |! Is_writeable ($ this-> path) {if (! Mkdir ($ this-> path) {Tool: alertBack ('warning: failed to create the main directory! ') ;}} If (! Is_dir ($ this-> today) |! Is_writeable ($ this-> today) {if (! Mkdir ($ this-> today) {Tool: alertBack ('warning: failed to create the subdirectory! ') ;}}// Verification type private function checkType () {if (! In_array ($ this-> type, $ this-> typeArr) {Tool: alertBack ('Warning: invalid Upload type! ') ;}} // Verification error private function checkError () {if (! Empty ($ this-> error) {switch ($ this-> error) {case 1: Tool: alertBack ('Warning: the upload value exceeds the agreed maximum! '); Break; case 2: Tool: alertBack ('Warning: the upload value exceeds'. $ this-> maxsize. 'KB! '); Break; case 3: Tool: alertBack (' warning: Only some files are uploaded! '); Break; case 4: Tool: alertBack (' warning: no file is uploaded! '); Break; default: Tool: alertBack (' warning: Unknown error! ') ;}}}?>

A static Tool class Tool. class. php is used. the code is as follows:

Tool. class. php

<? Php class Tool {// return static public function alertBack ($ _ info) {echo"

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.