laravel5.2 文件上传

视图层

<form method="post" action="{{URL('file')}}" enctype="multipart/form-data">
    <h1>添加新闻</h1>
    <table border="1">
        <tr>
            <th>新闻名称:</th>
            <th><input type="text" name="n_name"></th>
        </tr>
        <tr>
            <th>新闻分类:</th>
            <th><input type="text" name="n_lei"></th>
        </tr>
        <tr>
            <th>新闻内容:</th>
            <th><textarea name="n_content" id="" cols="20" rows="5"></textarea></th>
        </tr>
        <tr>
            <th>新闻图片:</th>
            <th><input type="file" name="n_file" /></th>
        </tr>
        <tr>
            <th>&nbsp;&nbsp;&nbsp;人:</th>
            <th><input type="text" name="n_author"></th>
        </tr>
        <tr>
            <th><input type="button" value="重置" /></th>
            <th><input type="submit" value="提交" /></th>
        </tr>
    </table>
</form>


控制器层

//文件上传
public function File(){
    //接受全部的值
    $users = input::get();
    //获取新闻名称的值
    $n_name = input::get('n_name');
    //获取新闻分类的值
    $n_lei = input::get('n_lei');
    //获取新闻内容的值
    $n_content = input::get('n_content');
    //获取添加人的值
    $n_author = input::get('n_author');
    //获取本地时间
    date_default_timezone_get();
    $n_time = date("Y-m-d H:i:s");
    //文件上传
    $n_file = input::file('n_file');
    if($n_file->isValid()){
        //获取文件名称
        $clientName = $n_file -> getClientOriginalName();
        $realPath = $n_file -> getRealPath();
        //获取图片格式
        $entension = $n_file -> getClientOriginalExtension();
        //图片保存路径
        $mimeTye = $n_file -> getMimeType();
        $path = $n_file -> move('storage/uploads');
    }
    //添加数据
    $str = DB::table('news')->insert(
        array('n_name'=>$n_name,'n_lei'=>$n_lei,'n_content'=>$n_content,'n_file'=>$path,'n_author'=>$n_author,'n_time'=>$n_time));
    //判断是否添加成功
    if($str){
        echo "<script>alert('提交成功');location.href='lists'</script>";
    }else{
        echo "<script>alert('提交失败');location.href='index'</script>";
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值