Indefinite quantity form generates thumbnails longer _php tutorials

Source: Internet
Author: User
Tags html form
Let's take a look at the example call method:

:

Here's a look at the code. First upload the class? !--? php
/*
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 whxbb () 21cn.com
writed:2005 NOV 20< br> modified:2006 MAR
Modify by Psdshow (Psdshow (at) yahoo.com.cn) 2007-11-30
LICENCE:GPL
Revision:v1. 0.2
*/
Class upload{
var $saveName;//Save name
var $savePath;//save path
var $fileFormat = array (' gif ', ' jpg ', ' do C ', ' application/octet-stream ');//file Format &mime
var $overwrite = 0;//override mode
var $maxSize = 0;//file Max bytes
var $e xt;//file extension
var $thumb = 0;//generates thumbnails
var $thumbWidth = 130;//thumbnail width
var $thumbHeight = 130;//thumbnail height
var $th Umbprefix = "_thumb_";//thumbnail prefix
var $errno;//error code
var $returnArray = array ();//return information for all files
var $returninfo = Arra Y ();//return information for each file


constructor function
@param $savePath File Save path
@param $fileFormat file format limit array
Maximum size @param $maxSize file
@param whether $overwriet overwrite 1 allow override 0 forbidden

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 the name of input in $fileInput Web Form (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 file 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 ']);//Get extension, assign to $this->ext, Next loop will update
$this->setsavename ($changeName = = 1? ": $ar [' name ']);//Set Save file name
if ($this->copyfile ($ar)) {
$this->returnarray[] = $this->returninfo;
}else{
$this->returninfo[' ERROR '] = $this->errmsg ();
$this->returnarray[] = $this->returninfo;
}
}
Return $this->errno? False:true;
}else{//uploading a single file
$this->getext ($fileArr [' name ']);//Get extension
$this->setsavename ($changeName = = 1? ": $fileArr [' name ']);//Set Save file name
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;
}
}

Single File Upload
Array of @param $fileArray 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 kilobytes
$this->returninfo[' type '] = $fileArray [' type '];
Check file format
if (! $this->validateformat ()) {
$this->errno = 11;
return false;
}
Check if the directory is writable
if (! @is_writable ($this->savepath)) {
$this->errno = 12;
return false;
}
If overwrite is not allowed, check if the file already exists
if ($this->overwrite = = 0 && @file_exists ($this->savepath. $fileArray [' name ']) {
$this->errno = 13;
return false;
//}
If there is a size limit, check if 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 thumbnails
$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 = +; 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 desired thumbnail, the copy
Move_uploaded_file ($this->savepath. $this->savename,
$this->savepath. $this->thumbprefix. $this->savename);
} else {
if ($originalWidth > $this->thumbwidth) {//w > Set width
$thumbWidth = $this->thumbwidth;
$thumbHeight = $this->thumbwidth * ($originalHeight/$originalWidth);
if ($thumbHeight > $this->thumbheight) {//h > Set height
$thumbWidth = $this->thumbheight * ($thumbWidth/$thumbHeight);
$thumbHeight = $this->thumbheight;
}
}elseif ($originalHeight > $this->thumbheight) {//h > Set height
$thumbHeight = $this->thumbheight;
$thumbWidth = $this->thumbheight * ($originalWidth/$originalHeight);
if ($thumbWidth > $this->thumbwidth) {//w > Set 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 =; return false;}
if (!imagecopyresampled ($createdThumb, $Original, 0, 0, 0, 0,
$thumbWidth, $thumbHeight, $originalWidth, $origi Nalheight))
{$this->errno = +; return false;}
if (! $SaveFunction ($createdThumb,
$this->savepath. $this->thumbprefix. $this->savename))
{$ This->errno = 22; return false;}

}
//delete temporary file
/*
if (!@ $this->del ($fileArray ["Tmp_name"]) {
return false;
}
*/
return true;
}

File format check, MIME detection
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;
}
Get file name extension
@param $fileName The original file name of the uploaded files
function Getext ($fileName) {
$ext = Explode (".", $fileName);
$ext = $ext [Count ($ext)-1];
$this->ext = Strtolower ($ext);
}

Set maximum byte limit for uploaded files
@param $maxSize File Size (bytes) 0: Indicates no limit
function Setmaxsize ($maxSize) {
$this->maxsize = $maxSize;
}
Set file format qualification
@param $fileFormat file Format array
function Setfileformat ($fileFormat) {
if (Is_array ($fileFormat)) {$this->fileformat = $fileFormat;}
}

Setting the Override mode
@param overwrite coverage mode 1: Allow overwrite 0: prohibit overwrite
function Setoverwrite ($overwrite) {
$this->overwrite = $overwrite;
}


Set Save path
@param $savePath File Save path: End With "/", If not "/", then fill in
function Setsavepath ($savePath) {
$this->savepath = substr (Str_replace ("\", "/", $savePath),-1) = = "/"
? $savePath: $savePath. " /";
}

Set the thumbnail image
@param $thumb = 1 produces a thumbnail $thumbWidth, $thumbHeight is the width and height of the thumbnail image
function Setthumb ($thumb, $thumbWidth = 0, $thumbHeight = 0) {
$this->thumb = $thumb;
if ($thumbWidth) $this->thumbwidth = $thumbWidth;
if ($thumbHeight) $this->thumbheight = $thumbHeight;
}

Set File Save name
@param $saveName Save name, if empty, the system automatically generates a random file name
function Setsavename ($saveName) {
if ($saveName = = ") {//If the file name is not set, a random file name is generated
$name = Date (' Ymdhis '). " _ ". Rand (100,999).". $this->ext;
Determine if a file exists and does not allow duplicate files
if (file_exists ($this->savepath. $name)) {
$name = Setsavename ($saveName);
}
} else {
$name = $saveName;
}
$this->savename = $name;
}

deleting files
@param $fileName The file name that you want to delete
Function del ($fileName) {
if (! @unlink ($fileName)) {
$this->errno = 15;
return false;
}
return true;
}

Return information for uploading files
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 is specified in the HTML form. ',
3 = ' The uploaded file is 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. ',
Ten = ' Input name is not unavailable! ',
One and the ' the uploaded file is unallowable! ',
' Directory unwritable! ',
+ = ' File exist already! ',
+ = ' File is too big! ',
\ = ' Delete file unsuccessfully! ',
+ = ' Your version of PHP does not appear to has GIF thumbnailing support. ',
+ = ' Your version of PHP does not appear to has JPEG thumbnailing support. ',
+ = ' Your version of PHP does not appear to has pictures thumbnailing support. ',
"An" error occurred while attempting to copy the source image.
Your version of PHP ('. phpversion (). ') may not have the this image type "support."
The "an" error occurred while attempting to create a new image. ',
"An" error occurred while copying the source image to the thumbnail image. ',
The "an" error occurred while saving the thumbnail image to the filesystem.
Is sure that PHP have been configured with both read and write access to this folder? ',
);
if ($this->errno = = 0)
return false;
Else
return $uploadClassError [$this->errno];
}
}
?>

Let's see how it's called.:

If you receive a parameter from the form, upload it, otherwise display the form
if (Isset ($_files[' uploadinput ')) {
$directoryname, where the parameter is not last "/",
If so, when you break the array with '/', a null value will appear at the end.
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 ("Cannot establish directory $temp");
Umask ($oldmask);
}
}
return true;
}

if ($_files[' uploadinput ' [' name '] <> ') {
Include Upload file class
Require_once (' class.upload.php ');
Set File upload Directory
$savePath = "Upload";
Create a Directory
Makedirectory ($savePath);
Types of files Allowed
$fileFormat = Array (' gif ', ' jpg ', ' jpge ', ' png ');
File size limit, Unit: byte,1kb = Byte
0 means no limit, but is affected by upload_max_filesize settings in php.ini
$maxSize = 0;
Overwrite the original file? 0 Not allowed 1 allowed
$overwrite = 0;
Initialize the Upload class
$f = new Upload ($savePath, $fileFormat, $maxSize, $overwrite);
If you want to generate thumbnails, call the member function $f->setthumb ();
Parameter list: Setthumb ($thumb, $thumbWidth = 0, $thumbHeight = 0)
$thumb =1 indicates that a thumbnail is to be generated, and when not called, its value is 0
$thumbWidth thumbnail width, in pixels (px), left blank with default value of 130
$thumbHeight thumbnail height, in pixels (px), with the default value of 130 if left blank
$f->setthumb (1);

The uploadinput in the parameter is the name of the input box of the upload file in the form
The following 0 means that the file name is not changed, and if 1, random file names are generated by the system
if (! $f->run (' Uploadinput ', 1)) {
Only the last error message can be obtained through $f->errmsg (),
Detailed information can be obtained in the $f->getinfo ().
echo $f->errmsg (). "
n ";
}
The upload results are saved in the array ReturnArray.
echo "

";
Print_r ($f->getinfo ());
echo "
";
}
}else{
?>

}
We upload a picture file that already exists,
A normal picture file, and a file that is not allowed to upload,
The output is as follows
/*
The uploaded file is unallowable!

Array
(
[0] = = Array
(
[Name] = Boy.jpg
[Savename] = boy.jpg
[Size] = 137
[Type] = Image/pjpeg
[ERROR] = File exist already!
)

[1] = = Array
(
[Name] = Girl. Jpg
[Savename] = girl. Jpg
[Size] = 31
[Type] = Image/pjpeg
[OriginalHeight] = 450
[OriginalWidth] = 600
)

[2] = = Array
(
[Name] = test.wma
[Savename] = test.wma
[Size] = 971
[Type] = Audio/x-ms-wma
[ERROR] = The uploaded file is unallowable!
)

)
*/
?>


http://www.bkjia.com/PHPjc/445081.html www.bkjia.com true http://www.bkjia.com/PHPjc/445081.html techarticle Let's take a look at the example call method:: Below to see the code. First Upload class?? PHP/* Title:class Upload FILE:class.upload.php description:to provide Upload utility, AUTHOR ...

  • 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.