An indefinite number of forms are generated for a long time. Let's take a look at the example and call the method: Let's take a look at the code. Upload the class first ?? Php * TITLE: CLASSUploadFILE: class. upload. phpDESCRIPTION: Toprovideuploadutility. AUTHOR:
:
Next let's take a look at the code. first upload the class? /*
TITLE: CLASS Upload
FILE: class. upload. php
DESCRIPTION: To provide upload utility,
AUTHOR: Peng Zhang zpadmin () gmail.com http://blog.neten.de
Based on: whxbb () 21cn.com
WRITED: 2005 NOV 20
MODIFIED: 2006 MAR 01
Modify by psdshow (at) yahoo.com.cn) 2007-11-30
LICENCE: GPL
REVISION: V1.0.2
*/
Class Upload {
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 = "_ thumb _"; // 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 Upload ($ 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;
} Else {// upload a single file
$ This-> getExt ($ fileArr ['name']); // get the extension
$ This-> setSavename ($ changeName = 1? '': $ FileArr ['name']); // sets the file name to be saved.
If ($ this-> copyfile ($ fileArr )){
$ This-> returnArray [] = $ this-> returninfo;
} Else {
$ This-> returninfo ['error'] = $ this-> errmsg ();
$ This-> returnArray [] = $ this-> returninfo;
}
Return $ this-> errno? False: true;
}
Return false;
} Else {
$ This-> errno = 10;
Return false;
}
}
// Upload a single file
// @ Param $ fileArray array of file information
Function copyfile ($ fileArray ){
$ This-> returninfo = array ();
// Return information
$ This-> returninfo ['name'] = $ fileArray ['name'];
$ This-> returninfo ['savename'] = $ this-> saveName;
$ This-> returninfo ['size'] = number_format ($ fileArray ['size'])/1024, 0 ,'. ', ''); // In KB
$ This-> returninfo ['type'] = $ fileArray ['type'];
// Check the file format
If (! $ This-> validateFormat ()){
$ This-> errno = 11;
Return false;
}
// Check whether the directory is writable
If (! @ Is_writable ($ this-> savePath )){
$ This-> errno = 12;
Return false;
}
// If overwriting is not allowed, check whether the file already exists.
// If ($ this-> overwrite = 0 & @ file_exists ($ this-> savePath. $ fileArray ['name']) {
// $ This-> errno = 13;
// Return false;
//}
// If the size limit exists, check whether the file exceeds the limit.
If ($ this-> maxSize! = 0 ){
If ($ fileArray ["size"]> $ this-> maxSize ){
$ This-> errno = 14;
Return false;
}
}
// File Upload
If (! @ Move_uploaded_file ($ fileArray ["tmp_name"], $ this-> savePath. $ this-> saveName )){
$ This-> errno = $ fileArray ["error"];
Return false;
} Elseif ($ this-> thumb) {// Create a thumbnail
$ CreateFunction = "imagecreatefrom". ($ this-> ext = 'jpg '? 'Jpeg ': $ this-> ext );
$ SaveFunction = "image". ($ this-> ext = 'jpg '? 'Jpeg ': $ this-> ext );
If (strtolower ($ CreateFunction) = "imagecreatefromgif"
&&! Function_exists ("imagecreatefromgif ")){
$ This-> errno = 16;
Return false;
} Elseif (strtolower ($ CreateFunction) = "imagecreatefromjpeg"
&&! Function_exists ("imagecreatefromjpeg ")){
$ This-> errno = 17;
Return false;
} Elseif (! Function_exists ($ CreateFunction )){
$ This-> errno = 18;
Return false;
}
$ Original =@$ CreateFunction ($ this-> savePath. $ this-> saveName );
If (! $ Original) {$ this-> errno = 19; return false ;}
$ OriginalHeight = ImageSY ($ Original );
$ OriginalWidth = ImageSX ($ Original );
$ This-> returninfo ['originalheight'] = $ originalHeight;
$ This-> returninfo ['originalwidth'] = $ originalWidth;
/*
If ($ originalHeight <$ this-> thumbHeight
& $ OriginalWidth <$ this-> thumbWidth )){
// If it is smaller than the expected thumbnail, Copy
Move_uploaded_file ($ this-> savePath. $ this-> saveName,
$ This-> savePath. $ this-> thumbPrefix. $ this-> saveName );
} Else {
If ($ originalWidth> $ this-> thumbWidth) {// width> set the width
$ ThumbWidth = $ this-> thumbWidth;
$ ThumbHeight = $ this-> thumbWidth * ($ originalHeight/$ originalWidth );
If ($ thumbHeight> $ this-> thumbHeight) {// height> sets the height.
$ ThumbWidth = $ this-> thumbHeight * ($ thumbWidth/$ thumbHeight );
$ ThumbHeight = $ this-> thumbHeight;
}
} Elseif ($ originalHeight> $ this-> thumbHeight) {// height> set height
$ ThumbHeight = $ this-> thumbHeight;
$ ThumbWidth = $ this-> thumbHeight * ($ originalWidth/$ originalHeight );
If ($ thumbWidth> $ this-> thumbWidth) {// width> set the width
$ ThumbHeight = $ this-> thumbWidth * ($ thumbHeight/$ thumbWidth );
$ ThumbWidth = $ this-> thumbWidth;
}
}
*/
$ Radio = max ($ originalWidth/$ this-> thumbWidth), ($ originalHeight/$ this-> thumbHeight ));
$ ThumbWidth = (int) $ originalWidth/$ radio;
$ ThumbHeight = (int) $ originalHeight/$ radio;
If ($ thumbWidth = 0) $ thumbWidth = 1;
If ($ thumbHeight = 0) $ thumbHeight = 1;
$ CreatedThumb = imagecreatetruecolor ($ thumbWidth, $ thumbHeight );
If (! $ CreatedThumb) {$ this-> errno = 20; return false ;}
If (! Imagecopyresampled ($ createdThumb, $ Original, 0, 0, 0, 0,
$ ThumbWidth, $ thumbHeight, $ originalWidth, $ originalHeight ))
{$ This-> errno = 21; return false ;}
If (! $ SaveFunction ($ createdThumb,
$ This-> savePath. $ this-> thumbPrefix. $ this-> saveName ))
{$ This-> errno = 22; return false ;}
}
// Delete a temporary file
/*
If (! @ $ This-> del ($ fileArray ["tmp_name"]) {
Return false;
}
*/
Return true;
}
// File format check and MIME check
Function validateFormat (){
If (! Is_array ($ this-> fileFormat)
| In_array (strtolower ($ this-> ext), $ this-> fileFormat)
| In_array (strtolower ($ this-> returninfo ['type']), $ this-> fileFormat ))
Return true;
Else
Return false;
}
// Obtain the file extension
// @ Param $ fileName the original file name to be uploaded
Function getExt ($ fileName ){
$ Ext = explode (".", $ fileName );
$ Ext = $ ext [count ($ ext)-1];
$ This-> ext = strtolower ($ ext );
}
// Sets the maximum byte limit for the uploaded file.
// @ Param $ maxSize file size (bytes) 0: unlimited
Function setMaxsize ($ maxSize ){
$ This-> maxSize = $ maxSize;
}
// Set the file format limitation
// @ Param $ fileFormat array
Function setFileformat ($ fileFormat ){
If (is_array ($ fileFormat) {$ this-> fileFormat = $ fileFormat ;}
}
// Sets the overwrite mode.
// @ Param overwrite mode 1: overwrite allowed 0: overwrite prohibited
Function setOverwrite ($ overwrite ){
$ This-> overwrite = $ overwrite;
}
// Set the save path
// @ Param $ savePath file storage path: ends with a slash (/). If no slash (/) exists, add
Function setSavepath ($ savePath ){
$ This-> savePath = substr (str_replace ("\", "/", $ savePath),-1) = "/"
? $ SavePath: $ savePath ."/";
}
// Set the thumbnail
// @ Param $ thumb = 1 generate a thumbnail $ thumbWidth, $ thumbHeight is the width and height of the thumbnail
Function setThumb ($ thumb, $ thumbWidth = 0, $ thumbHeight = 0 ){
$ This-> thumb = $ thumb;
If ($ thumbWidth) $ this-> thumbWidth = $ thumbWidth;
If ($ thumbHeight) $ this-> thumbHeight = $ thumbHeight;
}
// Set the file storage name
// @ Param $ saveName: Save name. if it is null, the system automatically generates a random file name.
Function setSavename ($ saveName ){
If ($ saveName = '') {// if no file name is set, a random file name is generated.
$ Name = date ('ymdhis '). "_". rand (100,999).'. '. $ this-> ext;
// Checks whether a file exists and does not allow duplicate files
If (file_exists ($ this-> savePath. $ name )){
$ Name = setSavename ($ saveName );
}
} Else {
$ Name = $ saveName;
}
$ This-> saveName = $ name;
}
// Delete an object
// @ Param $ fileName name of the file to be deleted
Function del ($ fileName ){
If (! @ Unlink ($ fileName )){
$ This-> errno = 15;
Return false;
}
Return true;
}
// Return the information of the uploaded object
Function getInfo (){
Return $ this-> returnArray;
}
// Get the error message
Function errmsg (){
$ UploadClassError = array (
0 => 'There is no error, the file uploaded with success .',
1 => 'The uploaded file exceeds The upload_max_filesize directive in php. ini .',
2 => 'The uploaded file exceeds The MAX_FILE_SIZE that was specified in the HTML form .',
3 => 'The uploaded file was only partially uploaded .',
4 => 'No file was uploaded .',
6 => 'missing a temporary folder. Introduced in PHP 4.3.10 and PHP 5.0.3 .',
7 => 'failed to write file to disk. Introduced in PHP 5.1.0 .',
10 => 'input name is not unavailable! ',
11 => 'The uploaded file is Unallowable! ',
12 => 'directory unwritable! ',
13 => 'File exist already! ',
14 => 'File is too big! ',
15 => 'Delete file unsuccessfully! ',
16 => 'Your version of PHP does not appear to have GIF thumbnailing support .',
17 => 'Your version of PHP does not appear to have JPEG thumbnailing support .',
18 => 'Your version of PHP does not appear to have pictures thumbnailing support .',
19 => 'A error occurred while attempting to copy the source image.
Your version of php ('. phpversion ().') may not have this image type support .',
20 => 'An error occurred while attempting to create a new image .',
21 => 'A error occurred while copying the source image to the thumbnail image .',
22 => 'A error occurred while saving the thumbnail image to the filesystem.
Are you sure that PHP has been configured with both read and write access on this folder? ',
);
If ($ this-> errno = 0)
Return false;
Else
Return $ uploadClassError [$ this-> errno];
}
}
?>
Let's take a look at how it is called .:
// 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 bytes
// 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 Upload ($ 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', 1 )){
// 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.
Echo"
";
print_r($f->getInfo());
echo "
";
}
} Else {
?>
}
// Upload an existing image file,
// A normal image file and an object that cannot be uploaded,
// The output result is as follows:
/*
The uploaded file is Unallowable!
Array
(
[0] => Array
(
[Name] => boy.jpg
[SaveName] => boy.jpg
[Size] = & gt; 137
[Type] => image/pjpeg
[Error] => File exist already!
)
[1] => Array
(
[Name] => girl. JPG
[SaveName] => girl. JPG
[Size] => 31
[Type] => image/pjpeg
[OriginalHeight] = & gt; 450
[OriginalWidth] = & gt; 600
)
[2] => Array
(
[Name] => test.wma
[SaveName] => test.wma
[Size] = & gt; 971
[Type] => audio/x-ms-wma
[Error] => The uploaded file is Unallowable!
)
)
*/
?>
Tips: Let's take a look at the code. first Upload class ?? Php/* TITLE: CLASS Upload FILE: class. upload. php DESCRIPTION: To provide upload utility, AUTHOR...