Blogger Information
Blog 15
fans 0
comment 0
visits 8375
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
补0820作业: 图片上传原生PHP整理
我们的关系如此狭窄
Original
467 people have browsed it

//前端

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="./layui/css/layui.css"  media="all">
</head>
<body>
<div style="text-align: center;">
    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
        <legend>常规使用:普通图片上传</legend>
    </fieldset>

    <div class="layui-upload">
        <button type="button" class="layui-btn" id="test1">上传图片</button>
        <div class="layui-upload-list">
            <img class="layui-upload-img" id="demo1">
            <p id="demoText"></p>
        </div>

    </div>
</div>

</body>
<script  src="http://libs.baidu.com/jquery/1.7.2/jquery.min.js"></script>
<script src="./layui/layui.js" charset="utf-8"></script>
<script>
    layui.use(['upload', 'element', 'layer'], function() {
        var $ = layui.jquery
            , upload = layui.upload
            , element = layui.element
            , layer = layui.layer;

        //常规使用 - 普通图片上传
        var uploadInst = upload.render({
            elem: '#test1'
            ,url: 'upload.php' //此处用的是第三方的 http 请求演示,实际使用时改成您自己的上传接口即可。
      
            , done: function (res) {
                console.log(res);
              
                return layer.msg(res.msg);

            }

        });
    })
</script>

</html>

//后端

$data = $_FILES;
//echo "<pre>";
//print_r($data);die;
function mkdirs($dir,$mode){
    return is_dir($dir) or (mkdirs(dirname($dir),$mode) and mkdir($dir,$mode));
}
function write_log($logName,$data){
        $date = date('Y-m');
        $dir = __DIR__.'/public/log/'.$date.'/';
        mkdirs($dir,$mode=0777);
        $fp = fopen($dir.$logName.'.log',"a");
        fwrite($fp,var_export($data,true)."\r\n");//写入文件
        fclose($fp);//关闭资源通道
}
function upload_pic_err_return($code){
    switch ($code){
        case 2001:
            $msg = '上传的图片格式不正确';
            break;
        case 2002:
            $msg = '上传的图片太大';
            break;
        case 2003:
            $msg = '上传的图片不合法';
            break;
        case 4004:
            $msg = '图片上传失败';
            break;
    }
    $data =['code'=>$code,'msg'=>$msg,'data'=>''];
    write_log('pic_upload_err',$data);
    echo json_encode($data);
    die;
}
//$dir= __DIR__.'/bs/bss/bbs';$mode = 0777;
//function mkdirs($dir,$mode){
//    if(is_dir($dir) || @mkdir($dir,$mode)) return  true;
//    if(!mkdirs(dirname($dir),$mode)) return false;
//    return @mkdir($dir,$mode);
//}
$dir = __DIR__.'/public/upload/pic/'.date('Y-m-d',time()).'/';
if(!is_dir($dir)){
    mkdirs($dir,0777);
}
$type = str_replace('image/','',$data['pic']['type']);
$allow_ext = [];
if(!in_array($type,$allow_ext))  upload_pic_err_return(2001);
if($data['pic']['size'] - 0 > 5000000) upload_pic_err_return(2002);
if(!getimagesize($data['pic']['tmp_name']))   upload_pic_err_return(2003);
$newFile = md5(time().$data['pic']['name']).'.'.$type;
if(!move_uploaded_file($data['pic']['tmp_name'],$dir.$newFile)) upload_pic_err_return(4004);
echo json_encode(['code'=>1001,'msg'=>'成功上传图片','data'=>'']) and write_log('pic_upload_suc',['code'=>1001,'msg'=>'成功上传图片','data'=>'']);


Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post