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

laravel框架单文件和多文件上传

2016-11-17 11:53 211 查看
laravel中单文件上传:

实现代码:

$file = Request::file('photo');//获取文件值  在此我们要在头部掉用use Illuminate\Http\Request;路由get传值
$name = input::get('name');//其他属性值
$allowed_extensions = ["png", "jpg", "gif"];
if ($file->getClientOriginalExtension() && !in_array($file->getClientOriginalExtension(), $allowed_extensions)) {
return ['error' => 'You may only upload png, jpg or gif.'];
}
$destinationPath = 'storage/uploads/'; //public 文件夹下面建 storage/uploads 文件夹
$extension = $file->getClientOriginalExtension();
$fileName = str_random(10).'.'.$extension;
$file->move($destinationPath, $fileName);
$filePath = asset($destinationPath.$fileName);
$info=DB::insert('insert into photo(pname,photo) VALUES (?,?)',[$name,$filePath]);//入库
if($info){
return Redirect('/show');
}else{
echo "no";
}
[/code]
在laravel中我没有找到多文件上传

在此我自己封装了一个多文件上传希望能帮到各位学友们;

朋友们路由我没写清楚 你们自己要注意点路由问题啦!

案例:

$file = $res->file("img_name");
foreach ($file as $key => $value) {
if(!empty($value)){//此处防止没有多文件上传的情况
$allowed_extensions = ["png", "jpg", "gif"];
if ($value->getClientOriginalExtension() && !in_array($value->getClientOriginalExtension(), $allowed_extensions)) {
return ['error' => 'You may only upload png, jpg or gif.'];die;
}
$destinationPath = 'storage/uploads/'; //public 文件夹下面建 storage/uploads 文件夹
$extension = $value->getClientOriginalExtension();
$fileName = str_random(10).'.'.$extension;//重命名
$value->move($destinationPath, $fileName);
$filePath = asset($destinationPath.$fileName);
$post['landlord_img']="storage/uploads/".$fileName;
$list=array('img_name'=>$fileName,'house_id'=>$id);
DB::table('img')->insert($list);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: