Here is a simple example:
CopyCode The Code is as follows: <?
// Http://www.jb51.net
Class upload {
Public $ length; // specify the file size.
Public $ file; // determines whether this class is used for Image Upload or file upload.
Public $ filename; // file name
Public $ filetemp; // upload a temporary file
Public $ filesize; // file size to be uploaded
Public $ error; // whether the file to be uploaded is wrong. PhP4 does not
Public $ filetype; // File Upload type
Public $ directory ;//
Public $ maxlen;
Public $ errormsg;
Function _ construct ($ length, $ file = true, $ directory)
{
$ This-> maxlen = $ length;
$ This-& gt; length = $ length * 1024;
$ This-> file = $ file; // true indicates the normal file, and false indicates the image judgment.
$ This-> directory = $ directory;
}
Public Function uploadfile ($ filefield)
{
$ This-> filename = $ filefield ['name'];
$ This-> filetemp = $ filefield ['tmp _ name'];
$ This-> error = $ filefield ['error'];
$ This-> filetype = $ filefield ['type'];
$ This-> filesize = $ filefield ['SIZE'];
$ Pathsign = directory_separator ;///
If ($ this-> file) // upload a general file
{
$ Path = $ this-> _ iscreateddir ($ this-> directory); // obtain the path
If ($ PATH) // http://www.jb51.net
{
$ Createfiletype = $ this-> _ getfiletype ($ this-> filename); // you can specify a file category.
$ Createfilename = uniqid (RAND (); // randomly generate a file name
$ Thisdir = $ this-> directory. $ pathsign. $ createfilename. ".". $ createfiletype;
If (@ move_uploaded_file ($ this-> filetemp, $ thisdir) // move the temporary file to the specified path
{
Return $ thisdir;
}
}
} Else {// Image Upload
$ Path = $ this-> _ iscreateddir ($ this-> directory); // obtain the path
If ($ PATH) // path exists // http://www.jb51.net
{
$ Createfiletype = $ this-> _ getfiletype ($ this-> filename); // you can specify a file category.
$ Createfilename = uniqid (RAND ());
Return @ move_uploaded_file ($ this-> filetemp, $ this-> directory. $ pathsign. $ createfilename. ".". $ createfiletype )? True: false;
}
}
}
Public Function _ isbig ($ length, $ fsize) // whether the returned file exceeds the specified size
{
Return $ fsize> $ length? True: false;
}
Public Function _ getfiletype ($ filename) // obtain the file suffix
{
Return end (explode (".", $ filename ));
}
Public Function _ isimg ($ filetype) // whether the Image Upload type is allowed
{
$ Type = array ("Jpeg", "GIF", "jpg", "BMP ");
$ Filetype = strtolower ($ filetype );
$ Filearray = explode ("/", $ filetype );
$ File_type = end ($ filearray );
Return in_array ($ file_type, $ type); // http://www.jb51.net
}
Public Function _ iscreateddir ($ PATH) // check whether the path exists. If it does not exist, create it.
{
If (! File_exists ($ PATH ))
{
Return @ mkdir ($ path, 0755 )? True: false; // permission 755 //
}
Else
{
Return true;
}
}
Public Function showerror () // Display Error Information
// Http://www.jb51.net
Echo "<script language = 'javascript '> \ n history. back (); \ n alert ('$ this-> errormsg'); \ n </SCRIPT> \ n ";
Exit ();
}
}
Class multiupload extends upload {
Public $ files;
Function _ construct ($ arrayfiles, $ length, $ file = true, $ directory)
{
$ This-> files = $ arrayfiles;
Parent: :__ construct ($ length, $ file, $ directory );
}
Function uploadmultifile ()
{
$ Arr = array ();
If ($ this-> _ judge () | $ this-> _ judge () = "no") // if all files meet the specifications, start uploading.
{
Foreach ($ this-> files as $ key => $ value)
{
$ Strdir = parent: uploadfile ($ this-> files [$ key]);
Array_push ($ arr, $ strdir );
}
// Http://www.jb51.net
Return $ arr;
} Else
{
Return false;
}
}
Function _ judge ()
{
If ($ this-> file)
{
Foreach ($ this-> files as $ key => $ value)
{
If ($ this-> _ isbig ($ this-> length, $ value ['SIZE'])
{
$ This-> errormsg = "file exceeds $ this-> maxlen K ";
Parent: showerror ();
}
If ($ value ['error'] = upload_err_no_file)
{
// $ This-> errormsg = "An error occurred while uploading files ";
// Parent: showerror ();
Return "no ";
}
}
Return true;
} Else
{
// Http://www.jb51.net
Foreach ($ this-> files as $ key => $ value)
{
If ($ this-> _ isbig ($ this-> length, $ value ['SIZE'])
{
$ This-> errormsg = "image exceeds $ this-> maxlen ";
Parent: showerror ();
}
If ($ value ['error']! = 0)
{
$ This-> errormsg = "An error occurred while uploading images ";
Parent: showerror ();
}
If (! $ This-> _ isimg ($ value ['type'])
{
$ This-> errormsg = "incorrect image format ";
Parent: showerror ();
}
}
Return true;
}
}
}
?>
A more complex PHP Upload class that can automatically generate thumbnails
Step 1:
Create folders and layout:
Annex: attachment (stores the uploaded original image in this directory)
|-Smallimg: stores thumbnail slices.
|-MARK: stores watermark images.
Include: stores class files, fonts (Local Program The Code uses 04b_082.16.ttf)
|-Upfile. php: integrates simple upload to generate thumbnails and watermark class file information
|-04b_082.16.ttf: Font File
Test. php: Test File
Step 2 upload class
Upfile. php Copy code The Code is as follows: <? PHP
Class upimages {
VaR $ attachment folder = "Annex"; // The attachment storage point. The default value is annex.
VaR $ smallfolder = "smallimg"; // path for storing thumbnails. Note: The path must be a subdirectory under $ Alibaba folder. The default value is smallimg.
VaR $ markfolder = "mark"; // watermark image storage location
VaR $ upfiletype = "jpg gif png"; // the upload type. Default Value: jpg gif png rar zip
VaR $ upfilemax = 1024; // The maximum upload size. The unit is "kb". The default value is kb.
VaR $ fonttype; // font
VaR $ maxwidth = 500; // maximum Image Width
VaR $ maxheight = 600; // maximum Image Height
Function upimages ($ inclufolder, $ smallfolder, $ includefolder ){
$ This-> folder = $ folder;
$ This-> smallfolder = $ smallfolder;
$ This-> fonttype = $ includefolder. "/04b_082.16.ttf ";
}
Function upload ($ inputname ){
$ Imagename = Time (); // set the current time to the image name
If (@ empty ($ _ FILES [$ inputname] ["name"]) Die (error ("no image information is uploaded, please confirm "));
$ Name = explode (".", $ _ FILES [$ inputname] ["name"]); // separate the files before upload with "." To obtain the file type
$ Imgcount = count ($ name); // obtain the number of interceptions.
$ Imgtype = $ name [$ imgCount-1]; // type of the retrieved File
If (strpos ($ this-> upfiletype, $ imgtype) === false) Die (error ("only supported File Upload types ". $ this-> upfiletype. "Not Supported ". $ imgtype ));
$ Photo = $ imagename. ".". $ imgtype; // name of the file written to the database
$ Uploadfile = $ this-> folder. "/". $ photo; // name of the uploaded file
$ Upfileok = move_uploaded_file ($ _ FILES [$ inputname] ["tmp_name"], $ uploadfile );
If ($ upfileok ){
$ Imgsize = $ _ FILES [$ inputname] ["size"];
$ Ksize = round ($ imgsize/1024 );
If ($ ksize> ($ this-> upfilemax * 1024 )){
@ Unlink ($ uploadfile );
Die (error ("uploaded file exceeds". $ this-> upfilemax. "kb "));
}
} Else {
Die (error ("failed to upload the image, please make sure that your upload file does not exceed $ upfilemax kb or the upload time has timed out "));
}
Return $ photo;
}
Function getinfo ($ photo ){
$ Photo = $ this-> specify folder. "/". $ photo;
$ Imageinfo = getimagesize ($ photo );
$ Imginfo ["width"] = $ imageinfo [0];
$ Imginfo ["height"] = $ imageinfo [1];
$ Imginfo ["type"] = $ imageinfo [2];
$ Imginfo ["name"] = basename ($ photo );
Return $ imginfo;
}
Function smallimg ($ photo, $ width = 128, $ Height = 128 ){
$ Imginfo = $ this-> getinfo ($ photo );
$ Photo = $ this-> getting folder. "/". $ photo; // obtain the image source
$ Newname = substr ($ imginfo ["name"], 0, strrpos ($ imginfo ["name"], "."). "_thumb.jpg"; // new image name
If ($ imginfo ["type"] = 1 ){
$ IMG = imagecreatefromgif ($ photo );
} Elseif ($ imginfo ["type"] = 2 ){
$ IMG = imagecreatefromjpeg ($ photo );
} Elseif ($ imginfo ["type"] = 3 ){
$ IMG = imagecreatefrompng ($ photo );
} Else {
$ IMG = "";
}
If (empty ($ IMG) return false;
$ Width = ($ width> $ imginfo ["width"])? $ Imginfo ["width"]: $ width;
$ Height = ($ height> $ imginfo ["height"])? $ Imginfo ["height"]: $ height;
$ Srcw = $ imginfo ["width"];
$ Srch = $ imginfo ["height"];
If ($ srcw * $ width> $ srch * $ height ){
$ Height = round ($ srch * $ width/$ srcw );
} Else {
$ Width = round ($ srcw * $ height/$ srch );
}
If (function_exists ("imagecreatetruecolor ")){
$ Newimg = imagecreatetruecolor ($ width, $ height );
Imagecopyresampled ($ newimg, $ IMG, 0, 0, 0, $ width, $ height, $ imginfo ["width"], $ imginfo ["height"]);
} Else {
$ Newimg = imagecreate ($ width, $ height );
Imagecopyresized ($ newimg, $ IMG, 0, 0, 0, $ width, $ height, $ imginfo ["width"], $ imginfo ["height"]);
}
If ($ this-> tofile ){
If (file_exists ($ this-> folder. "/". $ this-> smallfolder. "/". $ newname) @ unlink ($ this-> folder. "/". $ this-> smallfolder. "/". $ newname );
Imagejpeg ($ newimg, $ this-> exist folder. "/". $ this-> smallfolder. "/". $ newname );
Return $ this-> revoke folder. "/". $ this-> smallfolder. "/". $ newname;
} Else {
Imagejpeg ($ newimg );
}
Imagedestroy ($ newimg );
Imagedestroy ($ IMG );
Return $ newname;
}
Function watermark ($ photo, $ text ){
$ Imginfo = $ this-> getinfo ($ photo );
$ Photo = $ this-> specify folder. "/". $ photo;
$ Newname = substr ($ imginfo ["name"], 0, strrpos ($ imginfo ["name"], "."). "_mark.jpg ";
Switch ($ imginfo ["type"]) {
Case 1:
$ IMG = imagecreatefromgif ($ photo );
Break;
Case 2:
$ IMG = imagecreatefromjpeg ($ photo );
Break;
Case 3:
$ IMG = imagecreatefrompng ($ photo );
Break;
Default:
Return false;
}
If (empty ($ IMG) return false;
$ Width = ($ this-> maxwidth> $ imginfo ["width"])? $ Imginfo ["width"]: $ this-> maxwidth;
$ Height = ($ this-> maxheight> $ imginfo ["height"])? $ Imginfo ["height"]: $ this-> maxheight;
$ Srcw = $ imginfo ["width"];
$ Srch = $ imginfo ["height"];
If ($ srcw * $ width> $ srch * $ height ){
$ Height = round ($ srch * $ width/$ srcw );
} Else {
$ Width = round ($ srcw * $ height/$ srch );
}
If (function_exists ("imagecreatetruecolor ")){
$ Newimg = imagecreatetruecolor ($ width, $ height );
Imagecopyresampled ($ newimg, $ IMG, 0, 0, 0, $ width, $ height, $ imginfo ["width"], $ imginfo ["height"]);
} Else {
$ Newimg = imagecreate ($ width, $ height );
Imagecopyresized ($ newimg, $ IMG, 0, 0, 0, $ width, $ height, $ imginfo ["width"], $ imginfo ["height"]);
}
$ White = imagecolorallocate ($ newimg, 255,255,255 );
$ Black = imagecolorallocate ($ newimg, 0, 0, 0 );
$ Alpha = imagecolorallocatealpha ($ newimg, 230,230,230, 40 );
Imagefilledrectangle ($ newimg, 0, $ height-26, $ width, $ height, $ alpha );
Imagefilledrectangle ($ newimg, 13, $ height-20, 15, $ height-7, $ black );
Imagettftext ($ newimg, 4.9, 0, 20, $ height-14, $ black, $ this-> fonttype, $ text [0]);
Imagettftext ($ newimg, 4.9, 0, 20, $ height-6, $ black, $ this-> fonttype, $ text [1]);
If ($ this-> tofile ){
If (file_exists ($ this-> folder. "/". $ this-> markfolder. "/". $ newname) @ unlink ($ this-> folder. "/". $ this-> markfolder. "/". $ newname );
Imagejpeg ($ newimg, $ this-> composer folder. "/". $ this-> markfolder. "/". $ newname );
Return $ this-> exist folder. "/". $ this-> markfolder. "/". $ newname;
} Else {
Imagejpeg ($ newimg );
}
Imagedestroy ($ newimg );
Imagedestroy ($ IMG );
Return $ newname;
}
}
?>
Step 3: test the upload class
Test. php Copy code The Code is as follows: <? PHP
$ Export folder = "Annex ";
$ Smallfolder = "smallimg ";
$ Markfolder = "mark ";
$ Includefolder = "include ";
Require ("./". $ includefolder. "/upfile. php ");
$ IMG = new upimages ($ inclufolder, $ smallfolder, $ includefolder );
$ Text = array ("www.jb51.net", "All rights reserved ");
If (@ $ _ Get ["go"]) {
$ Photo = $ IMG-> upload ("upfile ");
$ IMG-> maxwidth = $ IMG-> maxheight = 350; // set the watermark image value.
$ IMG-> tofile = true;
$ Newsmallimg = $ IMG-> smallimg ($ photo );
$ Newmark = $ IMG-> watermark ($ photo, $ text );
Echo " <br> ";
Echo " <br> ";
Echo "<a href = './test. php'> continue upload </a> ";
} Else {
?>
<Form method = "Post" Action = "./test. php? Go = go "enctype =" multipart/form-Data ">
<Input type = "file" name = "upfile"> <br>
<Input type = "Submit" value = "Upload">
</Form>
<? PHP
}
?>