PHP100 excellent: Support for thumbnails for PHP Upload

Source: Internet
Author: User
// First create an upload class named uploadclassUpLoad {public $ error =; pubic $ uploadFile =; public $ mimeType = text/plain; public $ fiterType = array (); public $ Filter =; public $ fileSiz

Instance:

1. create the file upfile. php. the code is as follows:

// If a parameter is received from the form, upload the parameter; otherwise, the form is displayed.

If (isset ($ _ FILES ['uploadinput']) {

// Create a directory function. The $ directoryName parameter does not end "/",

// If yes, when '/' is split as an array, a null value is displayed.

Function makeDirectory ($ directoryName ){

$ DirectoryName = str_replace ("\", "/", $ directoryName );

$ DirNames = explode ('/', $ directoryName );

$ Total = count ($ dirNames );

$ Temp = '';

For ($ I = 0; $ I <$ total; $ I ++ ){

$ Temp. = $ dirNames [$ I]. '/';

If (! Is_dir ($ temp )){

$ Oldmask = umask (0 );

If (! Mkdir ($ temp, 0777) exit ("You cannot create a directory $ temp ");

Umask ($ oldmask );

}

}

Return true;

}

If ($ _ FILES ['uploadinput'] ['name'] <> ""){

// Contains the uploaded file class

Require_once ('class _ upload. php ');

// Set the file upload Directory

$ SavePath = "upload ";

// Create a directory

MakeDirectory ($ savePath );

// Allowed file types

$ FileFormat = array ('GIF', 'jpg ', 'jpg', 'PNG ');

// File size limit; unit: Byte, 1KB = 1000 Byte // php100.com

// 0 indicates no limit, but it is affected by the upload_max_filesize setting in php. ini.

$ MaxSize = 0;

// Overwrite the original file? 0 is not allowed. 1 is allowed.

$ Overwrite = 0;

// Initialize the upload class

$ F = new clsUpload ($ savePath, $ fileFormat, $ maxSize, $ overwrite );

// If you want to generate a thumbnail, call the member function $ f-> setThumb ();

// Parameter list: setThumb ($ thumb, $ thumbWidth = 0, $ thumbHeight = 0)

// $ Thumb = 1 indicates that a thumbnail is to be generated. when not called, the value is 0.

// $ ThumbWidth: width of the thumbnail. unit: Pixel (px). if it is left blank, the default value is 130.

// $ ThumbHeight the Thumbnail height in pixels (px). if it is left blank, the default value is 130.

$ F-> setThumb (1 );

// The uploadinput parameter is the name of the input field of the file to be uploaded in the form.

// The value 0 indicates that the file name is not changed. if it is 1, a random file name is generated by the system.

If (! $ F-> run ('uploadinput', 0 )){

// You can only get the last error message through $ f-> errmsg,

// Detailed information can be obtained in $ f-> getInfo.

Echo $ f-> errmsg ()."
\ N ";

}

// Save the upload result in the array returnArray. // Php100.com

Echo"

";

 

  print_r($f->getInfo());

  echo "

";

 

}

} Else {

?>

  

Send this file:
 

  
 

  
 

  
 

  
 

  

2. create the file class_upload.php. the code is as follows:

  

Class clsUpload {

Var $ saveName; // save the name

Var $ savePath; // save path

Var $ fileFormat = array ('GIF', 'jpg ', 'Doc', 'application/octet-stream'); // File Format & MIME limitation

Var $ overwrite = 0; // overwrite mode

Var $ maxSize = 0; // maximum file bytes

Var $ ext; // file extension

Var $ thumb = 0; // whether to generate a thumbnail

Var $ thumbWidth = 130; // Thumbnail width

Var $ thumbHeight = 130; // The height of the thumbnail.

Var $ thumbPrefix = "_"; // the prefix of the thumbnail.

Var $ errno; // error code

Var $ returnArray = array (); // return information of all objects

Var $ returninfo = array (); // information returned for each file

// Constructor

// @ Param $ savePath file storage path

// @ Param $ fileFormat file format limit array

// @ Param $ maxSize maximum file size

// @ Param $ overwriet indicates whether to overwrite 1. overwrite 0.

Function clsUpload ($ savePath, $ fileFormat = '', $ maxSize = 0, $ overwrite = 0 ){

$ This-> setSavepath ($ savePath );

$ This-> setFileformat ($ fileFormat );

$ This-> setMaxsize ($ maxSize );

$ This-> setOverwrite ($ overwrite );

$ This-> setThumb ($ this-> thumb, $ this-> thumbWidth, $ this-> thumbHeight );

$ This-> errno = 0;

}

// Upload

// @ Param $ fileInput name of input in Form

// @ Param $ changeName whether to change the file name

Function run ($ fileInput, $ changeName = 1 ){

If (isset ($ _ FILES [$ fileInput]) {

$ FileArr = $ _ FILES [$ fileInput];

If (is_array ($ fileArr ['name']) {// upload multiple files with the same file domain name

For ($ I = 0; $ I <count ($ fileArr ['name']); $ I ++ ){

$ Ar ['tmp _ name'] = $ fileArr ['tmp _ name'] [$ I];

$ Ar ['name'] = $ fileArr ['name'] [$ I];

$ Ar ['type'] = $ fileArr ['type'] [$ I];

$ Ar ['size'] = $ fileArr ['size'] [$ I];

$ Ar ['error'] = $ fileArr ['error'] [$ I];

$ This-> getExt ($ ar ['name']); // Obtain the extension and assign it to $ this-> ext. it will be updated in the next loop.

$ This-> setSavename ($ changeName = 1? '': $ Ar ['name']); // sets the file name to be saved.

If ($ this-> copyfile ($ ar )){

$ This-> returnArray [] = $ this-> returninfo;

} Else {

$ This-> returninfo ['error'] = $ this-> errmsg ();

$ This-> returnArray [] = $ this-> returninfo;

}

}

Return $ this-> errno? False: true;

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.