if(!isset($_session))session_start();
/**2010-6-22
* $data 数组类型 包含以下变量
-------------------------------
* $sub_type submit类型(upload:上传按钮 delete:删除按钮),默认upload
* $file 通过表单获取的$_files['filename']数组
* $img_tag_id 预览图片的的id值
* $img_dir 上传图片的目录
* $max_file_size 单位m(兆),默认:1m
* $type_array 允许的上传的图片类型(默认:image/pjpeg、image/jpeg、image/gif)
**/
function upload($data){
if(!$data['file']){
echo '';exit;
}
if(!$data['img_tag_id']){
echo '';exit;
}
if(!$data['img_dir']){
echo '';exit;
}
if(!isset($data['max_file_size'])){
$data['max_file_size'] = 1024 * 1024;
}else{
$data['max_file_size'] = $data['max_file_size'] * 1024 * 1024;
}
if(!isset($data['type_array'])){
$data['type_array'] = array('image/pjpeg', 'image/jpeg', 'image/gif');
}
if(!isset($data['sub_type'])){
$data['sub_type'] = 'upload';
}
$imgpath = '';
if(isset($data['sub_type']) && $data['sub_type'] == 'delete'){
if(isset($_session['name']) && $_session['name']){
if(is_file($_session['imgpath'])){
$b = unlink($_session['imgpath']);
}
unset($_session['name'], $_session['imgpath']);
if(!isset($_session['name'])){
echo '';
echo '';
}else{
echo '';
}
}else{
echo '';
}exit;
}
if(isset($_session['imgpath']) && $_session['imgpath']){
echo '';exit;
}
if(!in_array($data['file']['type'], $data['type_array'])){
echo '';exit;
}
if($data['file']['size'] > $data['max_file_size']){
echo '';exit;
}
if(!is_dir($data['img_dir'])){
@mkdir($data['img_dir'], 0777, true);
}
$imgpath = $data['img_dir'].'/'.date('his', time()).rand(100, 999).$data['file']['name'];
$isupload = move_uploaded_file($data['file']['tmp_name'], $imgpath);
if(!$isupload){
echo '';exit;
}else{
echo '';
}
$_session['name'] = $data['file']['name'];
$_session['imgpath'] = $imgpath;
return $imgpath;
}
/*test_start*/
$sub_type = '';
if(isset($_post['submit_upload']))$sub_type = 'upload';
if(isset($_post['submit_delete']))$sub_type = 'delete';
if($sub_type){//echo '';exit;
$data = array( 'sub_type' => $sub_type,
'file' => $_files['file'],
'img_tag_id' => 'picview',
'img_dir' => 'upload_img',
);
$imgpath = upload($data);
}else{
$imgpath = isset($_session['imgpath'])? $_session['imgpath']: '';
}
?>