您的位置:首页 > 编程语言 > PHP开发

laravel5.2 文件上传

2016-09-06 11:21 411 查看
视图层

<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>   人:</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>";
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: