-----------------------------------------uploads.class.php-------------------------------------------
<?php
class Uploads{
private $user_post_file=array(); //用户上传的文件
private $save_file_path; //用户上传文件存放的路径
private $max_file_size; //文件最大尺寸
private $last_error; //最后一次出错信息

//默认允许用户上传的文件类型
private $allow_type=array('gif','jpg','png','zip','rar','txt','doc','pdf');

private $final_file_path; //最终保存的文件名
private $save_info=array(); //返回一组有用的信息,用于提示用户
private $is_thumb="0"; //生成缩略图,默认为关闭状态
private $twidth="100"; //默认生成缩略图为100*100
private $theigh="100";
private $extend=".jpg"; //生成缩略图默认为.jpg

function __construct($file, $path, $size=2000000000, $type='', $thumb=0, $twidth=100, $theigh=100, $extend){
$this->user_post_file=$file;
$this->save_file_path=$path;
$this->max_file_size=$size; //如果用户不填写大小,则默认为2MB
$this->is_thumb=$thumb;
$this->twidth=$twidth;
$this->theight=$theigh;
$this->extend=$extend;
if($type!=''){
$this->allow_type=$type;
}
}
function upload(){
for($i=0;$i<count($this->user_post_file['name']);$i++){
if($this->user_post_file['error'][$i]==0){
//取当前文件名,临时文件名、大小、扩展名,后面将用到
$name=$this->user_post_file['name'][$i];
$tmpname=$this->user_post_file['tmp_name'][$i];
$size=$this->user_post_file['size'][$i];
$mime_type=$this->user_post_file['type'][$i];
$type=$this->getFileExt($this->user_post_file['name'][$i]);

//检查当前上传文件是否非法提交
if(!is_uploaded_file($tmpname)){
$this->last_error="非法上传文件";
$this->halt($this->last_error);
continue;
}
//判断上传文件的扩展名是否合法
if(!$this->checkType($type)){
$this->last_error="上传文件不合法:.".$type;
$this->halt($this->last_error);
continue;
}
//上传文件不得超过限定大小
if(!$this->checkSize($size)){
$this->last_error="上传文件不得超过限定大小";
$this->halt($this->last_error);
continue;
}
//重新给上传文件命名
$basename=$this->getBaseName($name,".".$type);
$saveas=$basename."-".time().".".$type; //移动后的文件名

//组合新文件名再存到指定目录下,格式:存储路径+文件名+时间+扩展名
if(!is_dir($this->save_file_path)){
mkdir($this->save_file_path,0777);
}
$this->final_file_path=$this->save_file_path ."/". $saveas;
if(!move_uploaded_file($tmpname,$this->final_file_path)){
$this->last_error=$this->user_post_file['error'][$i];
$this->halt($this->last_error);
continue;
}else{
if($this->is_thumb==1){
//判断是否要生成缩略图
$this->thumb($this->twidth,$this->theight,$this->final_file_path,$this->extend);
}
}
//存储上传文件相关的信息,以便后面使用
$this->save_info[]=array("name"=>$name,"type"=>$type,"mime_type"=>$mime_type,"size"=>$size,"saveas"=>$saveas,"path"=>$this->final_file_path);
}
}
return count($this->save_info); //返回上传成功的文件数目;
}
function getSaveInfo(){
//上传文件最终保存的路经
return $this->save_info;
}
function checkSize($size){
if($size>$this->max_file_size){
return false;
}else{
return true;
}
}
function checkType($extension){
//判断用户上传的类型是否合法
foreach($this->allow_type as $type){
if(strcasecmp($extension,$type)==0)
return true;
}
return false;
}
//显示报错信息
function halt($msg){
printf("<b><上传文件错误提示:></b> %s <br>\n",$msg);
}
function getFileExt($filename){
//获取上传文件的扩展名
$stuff=pathinfo($filename);
return $stuff['extension'];
}
function getBaseName($filename,$type){
//获取上传文件名(不包括扩展名)
$basename=basename($filename,$type);
return $basename;
}
private function thumb($twidth,$theight,$patch,$type){
//生成缩略图
switch($type){
case ".jpg":
$image2=imagecreatefromjpeg($patch);
if(!function_exists('outputimg')){
function outputImg($image,$path){
imagejpeg($image,$path);
}
}
break;

case ".gif":
$image2=imagecreatefromgif($patch);
if(!function_exists('outputimg')){
function outputImg($image,$path){
imagegif($image,$path);
}
}
break;

case ".png":
$image2=imagecreatefrompng($patch);
if(!function_exists('outputimg')){
function outputImg($image,$path){
imagepng($image,$path);
}
}
break;
}
$image2x=imagesx($image2); //获取原图大小
$image2y=imagesy($image2);
$bs=$image2x/$this->twidth;

if(($image2y/$bs)>$this->theigh){
//等比例缩放缩略图
$nbs=$image2y/$this->theigh;
$width=$image2x/$nbs;
$height=$this->theigh;
}elseif(($image2y/$bs)<$height){
$width=$this->twidth;
$height=$image2y/$bs;
}else{
$width=$image2x/$bs;
$height=$image2y/$bs;
}
$image=imagecreatetruecolor($width,$height);
if($type==".png"){
imagesavealpha($image2,true); //设置png透明alpha通道开启
imagealphblending($image,false);//不合并alpha通道图层
imagesavealpha($image,true);
}
$image2x=imagesx($image2);
$image2y=imagesy($image2);
imagecopyresampled($image,$image2,0,0,0,0,$width,$height,$image2x,$image2y);
outputimg($image,$patch);
imagedestroy($image2);
imagedestroy($image);
return true;
}
}
?>
-----------------------------------------upimg.php-------------------------------------------
<?php
header("Content-type:text/html;charset=utf-8");
require_once 'Uploads.class.php'; //引入上传类文件
if(isset($_POST['sub'])){
$type=array('gif','jpg','png','zip','rar'); //设置上传类型
$upload=new Uploads($_FILES['up'],'./up',100000000,$type,'1','200','200');
$num=$upload->upload();
if($num!=0){
echo $num."个文件上传成功!";
}else{
echo "文件上传失败!";
}
}
?><html>
<head>
<title>动态增加文件上传域</title>
<body>
<script type="text/javascript">
function addfile(){
var str='<input type="file" name="up[]" size="20"><br/>';
document.getElementById("myfile").insertAdjacentHTML("beforeEnd",str);
}
</script><form enctype="multipart/form-data" action="" method="post">
<input type="button" value="增行" onClick="addfile()"/><br/>
<a id="myfile">
<input type="file" name="up[]" ><br/>
</a>
<input type="submit" name="sub" value='批量上传'>
</form>
</body>
</html>错误提示:Warning: Missing argument 8 for Uploads::__construct(), called in D:\WWW\php100\caiji\upimg.php on line 6 and defined in D:\WWW\php100\caiji\uploads.class.php on line 18Warning: imagesx() expects parameter 1 to be resource, null given in D:\WWW\php100\caiji\uploads.class.php on line 146Warning: imagesy() expects parameter 1 to be resource, null given in D:\WWW\php100\caiji\uploads.class.php on line 147Warning: Division by zero in D:\WWW\php100\caiji\uploads.class.php on line 150Warning: Division by zero in D:\WWW\php100\caiji\uploads.class.php on line 155Warning: Division by zero in D:\WWW\php100\caiji\uploads.class.php on line 159Warning: Division by zero in D:\WWW\php100\caiji\uploads.class.php on line 160Warning: imagecreatetruecolor(): Invalid image dimensions in D:\WWW\php100\caiji\uploads.class.php on line 162Warning: imagesx() expects parameter 1 to be resource, null given in D:\WWW\php100\caiji\uploads.class.php on line 168Warning: imagesy() expects parameter 1 to be resource, null given in D:\WWW\php100\caiji\uploads.class.php on line 169Warning: imagecopyresampled() expects parameter 1 to be resource, boolean given in D:\WWW\php100\caiji\uploads.class.php on line 170Fatal error: Call to undefined function outputimg() in D:\WWW\php100\caiji\uploads.class.php on line 171

解决方案 »

  1.   

    i8 行
    function __construct($file, $path, $size=2000000000, $type='', $thumb=0, $twidth=100, $theigh=100, $extend){
    $extend 应写作 $extend='.jpg'$this->extend 只有一处赋值,默认是 .jpg
    你不管上传的图片是否为 .jpg,都按 .jpg 处理,这可以吗?
    如果可以,那为什么还要有 imagecreatefromjpeg、imagecreatefromgif、imagecreatefrompng 这些函数呢?