Multi-file Upload class implemented by PHP and examples of usage, _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
PHP implements multifile Upload classes and usage examples ,. The multifile Upload class implemented by PHP and its usage examples are described in this article. For your reference, refer to the multifile Upload class and usage example implemented by 1、upFiles.css. php PHP,

This example describes the multifile Upload class implemented by PHP and its usage. We will share this with you for your reference. The details are as follows:

1、upFiles.css. php file

<? Phpclass UploadFiles {private $ maxsize = '000000'; // maximum allowed Upload file length: private $ allowtype = array ('jpg ', 'PNG', 'GIF ', 'jpeg '); // The type of the file to be uploaded is private $ israndfile = true; // whether the random file name is private $ filepath; // The Upload path is private $ originName; // upload the source file private $ tmpfileName; // temporary file name private $ newfileName; // new file name private $ fileSize; // file size private $ fileType; // file type private $ errorNum = 0; // error code private $ errorMessg = array (); // error message // initialize a member Function _ construct ($ options = array () {foreach ($ options as $ key => $ val) {$ key = strtolower ($ key ); // check whether the subscript in the passed array is the same as the member attribute // print_r (array_keys (get_class_vars (get_class ($ this); if (! In_array ($ key, array_keys (get_class_vars (get_class ($ this) {continue;} else {$ this-> setOption ($ key, $ val );}}} private function setOption ($ key, $ val) {$ this-> $ key = $ val; // echo $ this-> errorNum."
";}// Check the file upload path private function checkfilePath () {// echo $ this-> filepath; if (empty ($ this-> filepath )) {$ this-> setOption ('errornum', "-5"); return false;} if (! File_exists ($ this-> filepath) |! Is_writable ($ this-> filepath) {if (! @ Mkdir ($ this-> filepath, 0755) {$ this-> setOption ('errornum', '-4'); return false ;}} return true ;} // Get error message private function getError () {$ str = "Upload file {$ this-> originName} error ---"; switch ($ this-> errorNum) {case 4; $ str. = "no file is uploaded"; break; case 3; $ str. = "only partially uploaded files"; break; case 2; $ str. = "exceeds the allowed file Form size"; break; case 1; $ str. = "exceeds php. allowed size in ini "; break; case-1; $ str. = "unsupported type"; break; case-2; $ str. = "The file is too large to exceed ". $ this-> maxsize. "byte"; break; case-3; $ str. = "Upload failed"; break; case-4; $ str. = "failed to create file Upload directory"; break; case-5; $ str. = "the upload path must be specified"; break; default; $ str. = "Unknown error";} return $ str."
";}// Check the file type private function checkfileType () {// echo $ this-> fileType; if (! In_array (strtolower ($ this-> fileType), $ this-> allowtype) {$ this-> setOption ('errornum', '-1'); return false ;} else {return true ;}/// check the file size. private function checkfileSize () {if ($ this-> fileSize> $ this-> maxsize) {$ this-> setOption ('errornum', '-2'); return false;} else {return true ;}// process random file name private function prorandFile () {$ ch = $ this-> israndfile; if ($ ch = 'true') {return true;} else {return fal Se ;}// private function setFiles ($ name = "", $ tmp_name = "", $ size = "", $ error = "") {// Check the upload path if (! $ This-> checkfilePath () {// $ this-> errorMessg = $ this-> getError (); return false;} // echo $ error ."
"; If ($ error) {$ this-> setOption ('errornum', $ error); return false;} $ arrstr = explode ('. ', $ name); $ type = end ($ arrstr); $ this-> setOption ('originname', $ name); $ this-> setOption ('filesize ', $ size); $ this-> setOption ('filetype', $ type); $ this-> setOption ('tmpfilename', $ tmp_name); return true ;} // check whether a file is uploaded. function checkFile ($ formname) {if (! @ $ _ FILES [$ formname]) {$ this-> setOption ('errornum', 4); return false;} else {return true ;}} // upload the file function uploadeFile ($ formname) {if (! $ This-> checkFile ($ formname) {$ this-> errorMessg = $ this-> getError (); return false;} $ return = true; $ name = @ $ _ FILES [$ formname] ['name']; $ tmp_name = @ $ _ FILES [$ formname] ['tmp _ name']; $ size = @ $ _ FILES [$ formname] ['size']; $ error = @ $ _ FILES [$ formname] ['error']; // $ type = $ _ FILES [$ formname] ['type']; if (is_array ($ name) {$ errors = array (); for ($ I = 0; $ I SetFiles ($ name [$ I], $ tmp_name [$ I], $ size [$ I], $ error [$ I]) {if (! $ This-> checkfileSize () |! $ This-> checkfileType () {$ errors [] = $ this-> getError (); $ return = false ;}} else {$ errors [] = $ this-> getError (); $ return = false;} if (! $ Return) $ this-> setFiles ();} if ($ return) {$ newfileN = array (); for ($ I = 0; $ I SetFiles ($ name [$ I], $ tmp_name [$ I], $ size [$ I], $ error [$ I]) {if (! $ This-> copyFile () {$ errors [] = $ this-> getError (); $ return = false ;} else {$ newfileN [] = $ this-> newfileName; }}$ this-> newfileName = $ newfileN ;}// print_r ($ errors ); $ this-> errorMessg = $ errors; // echo $ errors; return $ return;} else {if ($ this-> setFiles ($ name, $ tmp_name, $ size, $ error) {$ return = true; if ($ error) var_dump ($ error); if ($ this-> checkfileSize () & $ this-> checkfileType ()) {} else {$ return = False ;}} else {$ return = false;} if (! $ Return) {$ this-> errorMessg = $ this-> getError ();} return $ return ;}// obtain the uploaded file name function getnewFile () {return $ this-> newfileName;} // copy the file to the specified path function copyFile () {$ filepath = rtrim ($ this-> filepath ,'/'). "/"; if (! $ This-> errorNum) {if ($ this-> prorandFile () {$ this-> newfileName = date ('ymdhis '). rand (1000,9999 ). ". ". $ this-> fileType;} else {$ this-> newfileName = $ this-> originName;} if (! Move_uploaded_file ($ this-> tmpfileName, $ filepath. $ this-> newfileName) {$ this-> setOption ('errornum',-3); return false;} else {return true ;}} else {return false ;}} // function gteerror () {$ err = $ this-> errorMessg; return $ err ;}}?>

2. usage

Uploade. php file:

<? Php // print_r ($ _ FILES ['spic ']); header ('content-Type: text/html; charset = utf-8 '); // if (@ $ _ FILES ['spic ']) echo "ddddddddddd" implements include('upfiles.css. php '); $ upfile = new UploadFiles (array ('filepath' => '. /upload ', 'allowtype' => array ('php', 'bmp', 'GIF', 'jpg ', 'PNG'), 'israndfile' => true, 'maxsize' => '20140901'); if ($ upfile-> uploadeFile ('spic') {$ arrfile = $ upfile-> getnewFile (); foreach ($ arrfile as $ v) {echo $ v ,"
";} Echo" Upload successful! ";}Else {$ err = $ upfile-> gteerror (); if (is_array ($ err) {foreach ($ err as $ v1) {echo $ v1 ,"
";}} Else {echo $ err;} // var_dump ($ err);} // var_dump ($ upfile);?>

HTML file:

 Untitled Document

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.