°

PHP7实现数据添加及图片上传后重命名

2019年课程规划及价格说明


PHP实战视频教程大全
//文章添加函数
function add_post(){
    if(empty($_POST['title'] || empty($_POST['slug']) || empty($_POST['content']) || empty($_POST['category_id']) || empty($_POST['status']))){
        $GLOBALS['message'] = '数据不完整';
    }elseif (ft_fetch_all("select count(1) from ft_posts where sulg = {$_POST['slug']}")[0][0] > 0) {
        $GLOBALS['message'] = '别名已经存在';
    }else{
        preg_match('|\.(\w+)$|', $_FILES['img']['name'], $ext);
        $ext=$ext[0];//获取图片后缀用于重命名
        if(empty($_FILES['img']['error'])){
            $tmp_file = $_FILES['img']['tmp_name'];
            // $target_file = '../static/uploads/'.$_FILES['img']['name'];
            //修改图片保存路径及名称
            $target_file = '../static/uploads/'.date("Ymd").'/'.time().rand(100000, 999999).$ext;
            // $target_file = 'F:/wamp/www/fangtang/static/uploads/'.$_FILES['img']['name'];
            //检查文件夹是否存在,不存在就重新创建
            $imgSrc = '../static/uploads/'.date("Ymd");
            $dir = iconv("UTF-8", "GBK", $imgSrc);
            if (!file_exists($dir)){
                mkdir ($dir,0777,true);
            }
            if(move_uploaded_file($tmp_file, $target_file)){
                $img_src = $target_file;
            }else{
                $img_src = '';
            }
        }
        $title = $_POST['title'];
        $slug = $_POST['slug'];
        $category_id = $_POST['category_id'];
        $status = $_POST['status'];
        $content = $_POST['content'];
        $img = $img_src;
        $user_id = $_SESSION['current_login_user']['id'];
        $add_time = time();
        //添加数据
        $sql = sprintf("insert into ft_posts values(null,'%s','%s','%s','%d','%s', '%d','%d','%d','%d','%d')", $slug, $title, $img, $add_time,$content, 0, 0, $category_id, $user_id, $status);
        if(ft_sql_excute($sql) > 0){
            echo '插入数据成功!'; die;
        }else{
            echo '插入数据失败'; die;
        }
    }
    
}
打赏
  喜欢