I am new to laravel. I am trying to upload multiple files, but when I try to upload the same filenames at the same time, it crashes. how to prevent uploading same image names.
What I have tried is:
Html code:
<form method="POST" action="/listingSave" enctype="multipart/form-data" class="sellingFormSave" id="sellingFormSave" onsubmit="validate();">
<div class="row">
<div class="col-12 section_title-js">
<h3></h3>
</div>
<div class="form-group col-md-12">
<input id="demo" type="file" name="files" accept=".jpg, .png, image/jpeg, image/png" multiple>
</div>
</div>
<button type="submit" class="btn" id="submit">
Save
</button>
</form>
Controller.php
public function listingSave(Request $request) {
if(array_key_exists('image', $request->all())){
$imageName = $request->imageName;
$image = $request->image;
foreach ($image as $key => $value) {
$image_name = date('mdYhis').'_'.$imageName[$key];
$imgdata = base64_decode($value);
$myOutput = public_path().'/app/default/files-module/local/images/'.$image_name;
$ifp = fopen( $myOutput, 'wb' );
fwrite( $ifp, $imgdata );
fclose( $ifp );
$imageInfo = getimagesize($myOutput);
$files = FileModel::updateOrCreate(['name'=>$image_name],[
// 'sort_order'=>$truckian->id+$key,
'sort_order'=>0+$key,
'created_at'=>date('Y-m-d H:i:s'),
'updated_at'=>date('Y-m-d H:i:s'),
// 'created_by_id'=>Auth::user()->id,
'created_by_id'=>0,
'disk_id'=>1,
'folder_id'=>1,
'extension'=>str_replace('image/', '', $imageInfo['mime']),
'size'=>$imageInfo[0]*$imageInfo[1],
'mime_type'=>$imageInfo['mime'],
"entry_type" => "Anomaly\Streams\Platform\Model\Files\FilesImagesEntryModel",
"height" => $imageInfo[1],
"width" => $imageInfo[0],
]);
// DB::table('truckian_products_image')->insert(['entry_id'=>$truckian->id,'file_id'=>$files->id,'sort_order'=>$key+1]);
DB::table('truckian_products_image')->insert(['entry_id'=>$p_id,'file_id'=>$files->id,'sort_order'=>$key+1]);
}
foreach($available as $key => $value)
DB::insert('insert into default_truckian_mileage_gap(mileage_gap,number_of_products,truck_id)values (?, ?, ?)',[$key,$value,$p_id]);
}
}
Whenever I execute same file names, it crashes. How to fix this.
default_truckian_products_image(entry_id,file_id,sort_order) values (40, 8897, 2))default_truckian_products_imagetable.