php上传图片的方法

php上传图片

<?php
/**
 * @param {$fileInfo}   上传图片信息
 * 
 * php上传图片的方法
 */
function  uploadFile1($fileInfo)
{
    if (!is_null($fileInfo)) {
        //检查文件大小
        if ($fileInfo['size'] > 1000000) {
            return "上传文件大于1M";
        }
        //检查扩展名
        $allowExt = array('jpeg', "jpg", "png", "gif");
        $ext = strtolower(pathinfo($fileInfo['name'], PATHINFO_EXTENSION));
        if (!in_array($ext, $allowExt)) {
            return "不允许的扩展名";
        }
        //检查文件类型
        $allowMime = array("image/jpeg", "image/jpg", "image/png", "image/gif");
        if (!in_array($fileInfo['type'], $allowMime)) {
            return "上传文件类型错误";
        }
        //检查是否为真实图片
        if (!@getimagesize($fileInfo['tmp_name'])) {
            return "不是真实图片";
        }
        //上传文件的路径
        $uploadPath = "attachment/shopimg" . date("Ymd", time());
        if (!file_exists($uploadPath)) {
            @mkdir($uploadPath);
        }
        $uniName = md5(uniqid(microtime(true), true));
        $destination = $uploadPath . "/" . $uniName . "." . $ext;
        if (@move_uploaded_file($fileInfo['tmp_name'], $destination)) {
            return "上传成功";
        } else {
            return "上传失败";
        }
    } else {
        return "文件上传出错";
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章