今天要用写的图片上传类,大家提提意见,有初学朋友不嫌浅漏拷去用
<?
/*
//作者:sx83@163.com
//今天要用写的图片上传类,大家提提意见,有初学朋友不嫌浅漏拷去用
//测试
error_reporting(15);
$www='include';
if($_FILES['test']){
//单文件
$img=new SimpleImage($_FILES['test'],$www);
$result=$img->run();
if($result) {
echo("<img src=$www/$result>$www/$result");
}else {
echo(implode('<li>',$img->errors()));
}
}
//多文件上传使用array('size'=>$_FILES[size][0],'name'=>$_FILES['name'][0],'tmp_name'=>$_FILES['tmp_name'][0],'size'=>$_FILES['type'][0],)代$_FILES['test']
print <<<EOF
<form method=post action="" enctype="multipart/form-data">
<input type="file" name="test">
<input type="submit">
</form>
EOF;
*/
class SimpleImage {
var $cls_filename = "";
var $cls_tmp_filename = "";
var $cls_filetype = "";
var $cls_filesize=0;
var $cls_errors=array();
var $cls_upload_dir = "";
var $cls_max_filesize = 100;
var $cls_new_file_name='';
var $cls_file;
function SimpleImage($file,$dir,$max_size=0) {
$this->cls_file=$file;
$this->cls_upload_dir=$dir;
$this->cls_filename=@$this->cls_file['name'];
$this->cls_tmp_filename=@$this->cls_file['tmp_name'];
$this->cls_filetype=@$this->cls_file['type'];
$this->cls_filesize=@$this->cls_file['size']/1024;
if($max_size)$this->cls_max_filesize=$max_size;
$this->cls_new_file_name=$this->get_new_file_name();
}
//if upload secuss return new image name;
function run() {
if(!$this->is_image()){
$this->cls_errors[]="您只能上传图片";
Return ;
}
if(!$this->check_size()){
$this->cls_errors[]="文件过大,最在只能是 $this->cls_max_filesize k 您上传了 $this->cls_filesize k ";
Return ;
}
$is_run=@move_uploaded_file($this->cls_tmp_filename, $this->cls_upload_dir ."/". $this->cls_new_file_name);
if($is_run) {
Return $this->cls_new_file_name;
}else{
$this->cls_errors[]="文件上传出错文件".__FILE__."行".__LINE__."|NAME=$this->cls_filename|TMP_NAME=$this->cls_tmp_filename|UP_DIR:$this->cls_upload_dir";
}
}
//delete file;
function unlink_file() {
Return @unlink($this->cls_upload_dir . $this->get_new_name());
}
//check file size
function check_size() {
Return $this->cls_max_filesize > $this->cls_filesize;
}
//check file type
function is_image() {
Return preg_match('/image/',$this->cls_filetype);
}
//create new file name
function get_new_file_name() {
$file_ext_name=substr($this->cls_filetype,strrpos($this->cls_filetype,"/")+1);
$new_name=time();
$new_name.='_';
mt_srand (( double) microtime() * 1000000 );
for ( $i = 0; $i<5 ; $i++ ){
$new_name .= mt_rand (0,9);
}
Return $new_name.".".$file_ext_name;
}
//get errors array
function errors() {
Return $this->cls_errors;
}
}
?>
问题点数:20、回复次数:2Top
1 楼loveconan(放牛娃娃)回复于 2004-12-05 02:15:19 得分 10
再加点,比如生成缩略图之类的,就更有用了
:DTop
2 楼tt007((独孤求败--多情贱客无情贱))回复于 2004-12-05 10:08:46 得分 10
拿去试试,上次刚刚试了一个生成缩略图的类Top




