PHP 上传图片

来源:互联网 发布:教吉他的软件 编辑:程序博客网 时间:2024/05/16 20:31
        //本例运行环境 dedecms        //允许的文件类型        $allowtype = array("image/jpeg","image/jpg","image/gif","image/png","image/bmp");        $img = $_FILES['adimg'];        //图片错误        if(!in_array($img['type'],$allowtype))        {            ShowMsg('请选择图片!', '-1');            exit();        }        //用户信息        $cfg_ml = new MemberLogin();        $userid = $cfg_ml->M_ID;        //图片存储目录        $imgdir = DEDEROOT."/userunion/upimg/";        $monthdir = date("Ym",time());        $imgdir .= $monthdir."/";        //判断目录是否存在,不存在则创建        mkFolder($imgdir);        $filename=$img['name'];        $imgtype = substr($filename,strrpos($filename,"."));        //图片命名规则:时间戳_用户ID,确保图片永远不被覆盖        $imgurl = $imgdir.time()."_".$userid.$imgtype;        //移动临时图片        if(move_uploaded_file($img['tmp_name'],$imgurl))        {            $endtime = strtotime($endtime);            $sql = "insert into `#@__adcode`(adname,adcode,adtype,adsize,endtime,webname,adimg)                    values('$codename','$codecontent','图片','$codesize','$endtime','我的网站','$imgurl')";            if($dsql->ExecuteNoneQuery($sql))            {                ShowMsg('添加成功!','union_code.php');            }        }        else        {            ShowMsg('未知原因,图片上传失败','union_code.php');        }


判断目录是否存在的方法

//判断目录是否存在的方法function mkFolder($path){    if(!is_readable($path))    {        is_file($path) or mkdir($path,0700);    }}


原创粉丝点击