

附上代码:
public function save(){
$c_id = ForceIntFrom('c_id');
$r_id = ForceIntFrom('r_id'); //幻灯片调用ID
$title = ForceStringFrom('title');
$title1 = ForceStringFrom('title1');
$title1_en = ForceStringFrom('title1_en');
$title2 = ForceStringFrom('title2');
$title2_en = ForceStringFrom('title2_en');
$deletethisslide = ForceIntFrom('deletethisslide');
//上传文件类型列表
$uptypes=array(
'image/jpg',
'image/jpeg',
'image/png',
'image/pjpeg',
'image/gif',
'image/bmp',
'image/x-png'
);
$max_file_size=2097152; //上传文件大小限制, 单位BYTE
$destination_folder = ROOT."images/slide/"; //上传文件路径
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
if (!is_uploaded_file($_FILES["upfile"]["tmp_name"]))
//是否存在文件
{
$errors = "请选择要上传的图片!";
}
$file = $_FILES["upfile"];
if($max_file_size < $file["size"])
//检查文件大小
{
$errors = "您上传的图片太大!";
}
if(!in_array($file["type"], $uptypes))
//检查文件类型
{
$errors = "不允许的文件类型".$file["type"];
}
if(!file_exists($destination_folder))
{
mkdir($destination_folder);
}
$filename=$file["tmp_name"];
$image_size = getimagesize($filename);
$pinfo=pathinfo($file["name"]);
$ftype=$pinfo['extension'];
$destination = $destination_folder.time().".".$ftype;
if (file_exists($destination) && $overwrite != true)
{
$errors = "同名文件已经存在了";
}
if(!move_uploaded_file ($filename, $destination))
{
$errors = "移动文件出错";
}
$pinfo=pathinfo($destination);
$fname=$pinfo["basename"];
$save_dir="images/slide/".$fname;
}
if($deletethisslide AND $c_id){//删除幻灯片
if($c_id != 1 OR $c_id != 2) Error('抱歉, 系统默认的幻灯片无法删除!', '删除幻灯片错误'); //默认的两个幻灯片不能删除
$this->db->exe("DELETE FROM " . TABLE_PREFIX . "slide where c_id='$c_id'");
Success('slide');
}
if(!$title){
$errors[] = '幻灯片名称不能为空!';
}
if($c_id){//检查调用ID是否重复
if($this->db->getOne("SELECT c_id FROM " . TABLE_PREFIX . "slide WHERE r_id='$r_id' AND c_id != '$c_id'")){
$errors[] = '调用ID不能重复!';
}
}
if(isset($errors)) Error($errors, Iif($c_id, '编辑幻灯片错误', '添加幻灯片错误'));
if($c_id){
$this->db->exe("UPDATE " . TABLE_PREFIX . "slide SET
title = '$title',
title1 = '$title1',
title1_en = '$title1_en',
title2 = '$title2',
title2_en = '$title2_en',
r_id = '$r_id',
imagefile ='$save_dir'
WHERE c_id = ".$c_id);
Success('slide/edit?c_id=' . $c_id);
}else{
$time = time();
$getmax = $this->db->getOne("SELECT MAX(r_id) as max FROM " . TABLE_PREFIX . "slide");
$r_id = $getmax['max'] + 1;
$this->db->exe("INSERT INTO " . TABLE_PREFIX . "slide (title, title1, title1_en, title2, title2_en, imagefile, created, r_id) VALUES ('$title', '$title1', '$title1_en', '$title2', '$title2_en', '$save_dir', '$time', '$r_id') ");
$lastid = $this->db->insert_id;
Success('slide/edit?c_id=' . $lastid);
}
} 最佳答案