Php 以不同属性上载多个文件
如何在MYSQL中上传不同属性的多个选定文件,如pic_1、pic_2、pic_3 观点Php 以不同属性上载多个文件,php,laravel,file-upload,laravel-5.8,Php,Laravel,File Upload,Laravel 5.8,如何在MYSQL中上传不同属性的多个选定文件,如pic_1、pic_2、pic_3 观点 控制器 $this->validate($request[ “carEvidence”=>“必需”| mimes:jpeg、png、jpg、zip、pdf | max:2048”, ]); $post=新车; 如果($files=$request->file('carEvidence')){ $destinationPath='public/image/';//上载路径 $profileImage=da
控制器
$this->validate($request[
“carEvidence”=>“必需”| mimes:jpeg、png、jpg、zip、pdf | max:2048”,
]);
$post=新车;
如果($files=$request->file('carEvidence')){
$destinationPath='public/image/';//上载路径
$profileImage=date('YmdHis')。“..$files->getClientOriginalExtension();
$files->move($destinationPath,$profileImage);
$post['carEvidence']=“$profileImage”;
}
$post->save();
返回重定向('/internalaudit')->带有('success','Has sent for Validation');
仅为此类元素提供多个
属性并不是您所需要的一切。创建多输入类型时,无论是文件
、选择
还是输入
,您都需要在名称后面加上[]
,因此在您的示例中,该字段将为:
<input type="file" name="carEvidence[]" multiple>
从那里你可以循环浏览并适当地上传它们。除了
@ollieread
的答案之外,你还可以循环浏览数组,将图像或任何其他上传文件存储在数据库或任何地方。
要获取多个文件
<input type = "file" name = "carEvidence[]" multiple>
简单。请尝试并让我知道。按照以下代码进行操作:
在视图文件中
-
我认为,不同的属性,例如用于汽车的picu 1、picu 2、picu 3
,单个表会使您难以处理文件。我的建议是,使用关系。如果是这样,您正在将有关汽车的一些信息存储到cars
表中。每辆车可能有多个图像显示为汽车证据。在这种情况下,最好创建另一个表,例如car\u证据
在此表中创建两列,分别命名为cars\u id
和car\u图像
。然后在他们之间建立关系。如果您喜欢这样做,那么您可以根据需要动态提交更多的汽车图像pic_1、pic_2、pic_3属性不是更好的方法。请看一下我的流程-
您的Cars.php模型-
class Cars extends Model
{
protected $primaryKey = 'cars_id';
protected $fillable = [
//your cars table's other fields
];
public function carevidence()
{
return $this->hasMany('App\CarEvidence', 'cars_id', 'id');
}
}
通过运行php artisan Make:model CarEvidence-m
进行迁移,创建一个新模型CarEvidence
。然后,在这个迁移文件中只需添加两个列
,如下所示-
$table->bigInteger('cars_id')->unsigned()->nullable();
$table->string('car_images');
您的CarEvidence.php模型应该如下所示-
class CarEvidence extends Model
{
protected $fillable = [
'cars_id', 'car_images',
];
public function car(){
return $this->belongsTo('App\Cars', 'cars_id', 'id');
}
}
表格是-
<form action="your-route-or-url-here" method="post" enctype="multipart/form-data">
@csrf
//your other input fields about car information here, I think if any
<input type="file" name="carEvidence[]" multiple>
<button type="submit">Save</button>
</form>
然后在控制器方法中-
public function YourMethodHere(Request $request){
$this->validate($request, [
'carEvidence.*' => 'required|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
$post = new Cars;
//$post->carEvidence = "yes";here other information about car to save in `cars` table
if ($post->save()) {
if($request->hasFile('carEvidence')){
$files = $request->file('carEvidence');
foreach($files as $file){
$extension = $file->getClientOriginalExtension();
$filename =time().'.'.$extension;
$file->move(public_path('/image'), $filename);
CarEvidence::create([
'cars_id' => $post->id,
'car_images' => $filename
]);
}
}
}
return redirect('/internalaudit')->with('success', "Has been sent for Validation");
}
最后,在返回刀片文件时
public function yourmethodname()
{
$cars = Cars::with('carevidence')->get();
return view('your-view-blade-file', compact('cars'));
}
然后在查看刀片文件的内部
获取汽车图像(证据),您可以使用嵌套的foreach
循环-
@if (session('success'))
<div class="alert alert-success">{{ session('success') }}</div>
@endif
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
@foreach($cars as $car)
<p>{{ $car->your-fields-from-cars-table }}</p>
//and get all relevant images for car by this
@foreach($car->carevidence as $evidence)
<img src="{{ asset('image/'.$evidence->car_images) }}" height="60px" width="60px" />
@endforeach
@endforeach
@if(会话('success'))
{{session('success')}
@恩迪夫
@如果($errors->any())
@foreach($errors->all()作为$error)
- {{$error}}
@endforeach
@恩迪夫
@foreach($cars作为$car)
{{$car->cars表中的字段}
//并通过此按钮获取汽车的所有相关图像
@foreach($car->carevidence作为$证据)
汽车图片)}}“height=“60px”width=“60px”/
@endforeach
@endforeach
我希望这会对您有所帮助!您的输入type=“file”需要命名为carEvidence[]
接受多个文件作为数组。然后使用foreach
循环遍历数组,然后逐个存储。如果问题解决,我会接受答案!:)@Steven属性表示不同的数据库列。如pic_1、pic_2、pic_3或其他..@amitsenjalia yes broIt将是name=“carEvidence[]“
好的,我会尽力让你知道:)谢谢!谢谢你,兄弟,真管用!:)将在5小时内颁发赏金!我很高兴它对你有用。不客气。:)
public function yourmethodname()
{
$cars = Cars::with('carevidence')->get();
return view('your-view-blade-file', compact('cars'));
}
@if (session('success'))
<div class="alert alert-success">{{ session('success') }}</div>
@endif
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
@foreach($cars as $car)
<p>{{ $car->your-fields-from-cars-table }}</p>
//and get all relevant images for car by this
@foreach($car->carevidence as $evidence)
<img src="{{ asset('image/'.$evidence->car_images) }}" height="60px" width="60px" />
@endforeach
@endforeach