图片上传

白俊遥博客

BaseService:

<?php
/**
 * Created by PhpStorm.
 * User: HIAPAD
 * Date: 2019/1/9
 * Time: 17:18
 */

namespace app\common\Services;


class BaseService
{
    protected static $_error_msg = null;
    protected static $_error_code = null;
    public static function _err($msg='',$code = -1){
        if($msg){
            self::$_error_msg = $msg;
        }else{
            self::$_error_msg = '操作失败';
        }
        self::$_error_code = $code;
        return false;
    }

    public static function getLastErrorMsg(){
        return self::$_error_msg?self::$_error_msg:"";
    }


    public static function getLastErrorCode(){
        return self::$_error_code?self::$_error_code:0;
    }

}

UploadService:

<?php
/**
 * 文件上传
 * User: HIAPAD
 * Date: 2019/1/9
 * Time: 17:16
 */

namespace app\common\Services;


//上传服务
class UploadService extends BaseService
{
    //根据文件路径上传
    public static function uploadByFile($file_name,$file_path,$bucket = ''){
        if ( !$file_name ){
            return self::_err("参数文件名是必须的~~");
        }

        if ( !$file_path || !file_exists( $file_path )){
            return self::_err("请输入合法的file_path~~");
        }

        $upload_config = \Yii::$app->params['upload'];
        if ( !isset($upload_config[ $bucket ])){
            return self::_err("指定参数backet错误~~");
        }

        $tmp_file_extend = explode(".",$file_name);
        $file_type = strtolower( end($tmp_file_extend) );

        //在每个篮子下面,按照日期来存储
        $hash_key = md5( file_get_contents( $file_path ));
        $upload_dir_path = UtilService::getRootPath() . "/web" . $upload_config[$bucket]."/";
        $folder_name = date("Ymd");
        $upload_dir = $upload_dir_path.$folder_name;
        if (!file_exists($upload_dir)){
            mkdir($upload_dir,0777,true);
            chmod($upload_dir,0777);
        }
        $upload_full_name = $folder_name."/".$hash_key.".{$file_type}";

        if ( is_uploaded_file( $file_path )){
            if (!move_uploaded_file($file_path,$upload_dir_path.$upload_full_name)){
                return self::_err("上传失败!系统繁忙请稍后再试!");
            }
        }else{
            file_put_contents($upload_dir_path.$upload_full_name,file_get_contents($file_path));
        }

        return [
            'code' => 200,
            'path' => $upload_full_name,
            'prefix' => $upload_config[$bucket]."/"
        ];
    }

}


UploadController:

<?php
/**
 * Created by PhpStorm.
 * User: HIAPAD
 * Date: 2019/1/9
 * Time: 16:33
 */

namespace app\modules\web\controllers;

use app\modules\web\controllers\common\BaseController;
use app\common\Services\UploadService;


class UploadController extends BaseController
{
    protected  $allow_file = ["gif","jpg","png","jpeg"];

    public function actionPic(){
        $uid = $this->post('uid',0);
        if (!$uid){
            $uid = $this->getUid();
        }
        $bucket = trim($this->post("bucket",""));
        $type = $this->post("type");

        $call_back_target = 'window.parent.upload';

        if ( !$uid ){
            return "<script type='text/javascript'>{$call_back_target}.error('系统繁忙请稍后再试');</script>";
        }
        if (!$_FILES || !$_FILES['pic']){
            return "<script type='text/javascript'>{$call_back_target}.error('没有选择文件');</script>";
        }

        $file_name = $_FILES['pic']['name'];
        $tmp_file_extend = explode(".",$file_name);
        if (!in_array( strtolower(end($tmp_file_extend)),$this->allow_file)){
            return "<script type='text/javascript'>{$call_back_target}.error('请上传图片文件,jpg,png,jpeg,gif');</script>";
        }
        $ret = UploadService::uploadByFile( $_FILES['pic']['name'],$_FILES['pic']['tmp_name'],$bucket );

        if( !$ret ){
            return "<script type='text/javascript'>{$call_back_target}.error('".UploadService::getLastErrorMsg()."');</script>";
        }
        return "<script type='text/javascript'>{$call_back_target}.success('{$ret['path']}','$type');</script>";
    }

}


白俊遥博客
请先登录后发表评论
  • 最新评论
  • 总共0条评论