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 "文件上傳出錯";
    }
}

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