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/';
$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";
}
在laravel中我没有找到多文件上传
在此我自己封装了一个多文件上传希望能帮到各位学友们;
朋友们路由我没写清楚 你们自己要注意点路由问题啦!
案例:
- <span style="font-size:18px;"> $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);
- }
- }</span>