一个比较简单的PHP上传的例子,但基本控制了图片类型和上传大小的类
<?php
/*
* Programmer : Msn/QQ haowubai@hotmail.com (925939)
* www.php100.com Develop a project PHP - MySQL - Apache
* Window 2003 - Preferences - PHPeclipse - PHP - Code Templates
*/
/**
* 图片上传方法
* $maxsize=500000;
* $updir="up/";
* $upfile=$_FILES["file_img"];
*/
function _UPLOADPIC ($upfile,$maxsize,$updir,$newname='date'){
if($newname=='date') $newname=date("Ymdhs"); //使用日期做文件名
$name = $upfile["name"];
$type = $upfile["type"];
$size = $upfile["size"];
$tmp_name = $upfile["tmp_name"];
switch($type){
case 'image/pjpeg' :
case 'image/jpeg' : $extend=".jpg"; break;
case 'image/gif' : $extend=".gif"; break;
case 'image/png' : $extend=".png"; break;
}
if (empty($extend)) {
echo _MESSAGE("警告!只能上传图片类型:GIF JPG PNG");
exit;
}
if ($size > $maxsize) {
$maxpr=$maxsize/1000;
echo _MESSAGE("警告!上传图片大小不能超过".$maxpr."K!");
exit;
}
if (move_uploaded_file($tmp_name, $updir.$newname.$extend)) {
return $updir.$newname.$extend;
}
}
?>