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"