Php file Upload class code _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Php file upload code. [Php] * upload a single file function upload a file configuration with the suffix allowed by the configuration to get the file suffix to determine the file suffix error * classUpTool {protected $ allowExtjp [php]

/*

Upload a single file

Function

Upload files

Configure the allowed suffixes

Configure the allowed size

Get file suffix

Determine the file suffix

Error

*/

Class UpTool {

Protected $ allowExt = 'jpg, jpeg, gif, bmp, png ';

Protected $ maxSize = 1; // 1 M, in MB

Protected $ file = null; // prepare to store uploaded file information

Protected $ errno = 0; // error code

Protected $ error = array (

0 => 'Error-free ',

1 => 'upload file size exceeds system limited ',

2 => 'size of the uploaded file exceeds the webpage form limited ',

3 => 'Only part of the file is upload ',

4 => 'No file is upload ',

6 => 'temporary folder not found ',

7 => 'File write failed ',

8 => 'File suffixes not allowed ',

9 => 'File size exceeds the permitted range of the class ',

10 => 'directory creation failed ',

11 => 'File moving failed'

);

/*

Upload

*/

Public function up ($ key ){

If (! Isset ($ _ FILES [$ key]) {

Return false;

}

$ F = $ _ FILES [$ key];

// Check whether the upload is successful

If ($ f ['error']) {

$ This-> errno = $ f ['error'];

Return false;

}

// Obtain the suffix

$ Ext = $ this-> getExt ($ f ['name']);

// Check the suffix

If (! $ This-> isAllowExt ($ ext )){

$ This-> errno = 8;

Return false;

}

// Check the size

If (! $ This-> isAllowSize ($ f ['size']) {

$ This-> errno = 9;

Return false;

}

// Create a directory

$ Dir = $ this-> mk_dir ();

If ($ dir = false ){

$ This-> errno = 10;

Return fasle;

}

// Generate random file names

$ Newname = $ this-> randName (). '.'. $ ext;

// $ Dir = $ dir. '/'. $ newname;

// Move

If (! Move_uploaded_file ($ f ['tmp _ name'], $ dir. '/'. $ newname )){

$ This-> errno = 11;

Return false;

}

Return true; // str_replace (ROOT, '', $ dir );

}

Public function getErr (){

Return $ this-> error [$ this-> errno];

}

/*

Extensions allowed by parm string $ exts

Automatically add allowed suffixes and file sizes

*/

Public function setExt ($ exts ){

$ This-> allowExt = $ exts;

}

Public function setSize ($ num ){

$ This-> maxSize = $ num;

}

/*

String $ file

Return string $ ext suffix

*/

Protected function getExt ($ file ){

$ Tmp = explode ('.', $ file );

Return end ($ tmp );

}

/*

String $ ext file suffix

Return bool

Case sensitivity protection

*/

Protected function isAllowExt ($ ext ){

Return in_array (strtolower ($ ext), explode (',', strtolower ($ this-> allowExt )));

}

/*

Check the file size

*/

Protected function isAllowSize ($ size ){

Return $ size <= $ this-> maxSize * 1024*1024;

}

// Create a directory by date

Protected function mk_dir (){

$ Dir = 'Images/'. date ('ym/D ');

If (is_dir ($ dir) | mkdir ($ dir, 0777, true )){

Return $ dir;

} Else {

Return false;

}

}

/*

Generate random file names

*/

Protected function randName ($ length = 6 ){

$ Str = 'abcdefghijkmnpqrstuvwxyz23456789 ';

Return substr (str_shuffle ($ str), 0, $ length );

}

}

Form

[Html]

Initiate another page call

[Php]

Require ('./UpTool. class. php ');

$ Uptool = new UpTool ();

$ Uptool-> setExt ('rar, doc ');

$ Uptool-> setSize (1 );

If ($ uptool-> up ('Pic ')){

Echo 'upload successful ';

} Else {

Echo 'failed ';

Echo $ uptool-> getErr ();

}

Upload/* upload of a single file function Upload file configuration allows the suffix configuration to allow the size to obtain the file suffix to determine the file suffix error */class UpTool {protected $ allowExt = jp...

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.