PHP File Upload instance details,
This example describes the PHP File Upload class. 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 "<script type = 'text/javascript '> alert (' $ _ info '); history. back (); </script> "; exit () ;}// close the static public function alertOpenerClose ($ _ info, $ _ path) {echo "<script type = 'text/javascript '> alert (' $ _ info'); </script> "; echo "<script type = 'text/javascript '> opener.doc ument. content. thumbnail. value = '$ _ path'; </script> "; echo" <script t Ype = 'text/javascript '> opener.doc ument. content. pic. style. display = 'block'; </script> "; echo" <script type = 'text/javascript '> opener.doc ument. content. pic. src = '$ _ path'; </script> "; echo" <script type = 'text/javascript'> window. close (); </script> "; exit () ;}}?>
The following is an example. See the following steps:
1. Create an index. php page and create a form.
Index. php
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2. Create an upfile.html file, create a form, and submit it to upload. php.
Upfile.html
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3. Use the upload. php file to call the File upload class for upload, and assign the path to the input label and display image.
<? Php require 'fileupload. class. php '; if (isset ($ _ POST ['send']) {$ _ fileupload = new FileUpload ('pic ', $ _ POST ['max _ FILE_SIZE ']); $ _ path = $ _ fileupload-> getPath (); Tool: alertOpenerClose ('file uploaded successfully! ', $ _ Path);} else {Tool: alertBack (' warning: the file is too large or other unknown errors cause the browser to crash! ') ;}?>