我不知道为什么我的Excel工作表上传空了我的控制器功能
public function uploadedCars(Request $request)
{
if (!$request->hasFile('file'))
return Response::json(['error' => 'No File Sent.']);
if (!$request->file('file')->isValid())
return Response::json(['error' => 'File is not valid']);
$file = $request->file('file');
$extension = $file->getClientOriginalExtension();
if ($extension == "csv" || $extension == "CSV" || $extension == "xlsx" || $extension == "XLSX"
|| $extension == "rtf" || $extension == "rtf" || $extension == "ods" || $extension =="ODS"){}
else {
$status = 'sorry this file extension is not allowed';
return Redirect::back()->with(session()->flash('Error_flash_message', $status));
}
// $file = time().'.'.$request->file->getClientOriginalExtension();
$path = $file->move(public_path('uploads'), $file);
$data = Excel::load($path, function($reader) {
})->get();
if(!empty($data) && $data->count()){
foreach ($data as $key => $value) {
$insert[] = [
'cat_id' => $value->cat_id, 'car_name' => $value->car_name
,'starting_price' => $value->starting_price
,'image' => $value->image , 'car_name_ar' => $value->car_name_ar
,'year_start' => $value->year_start , 'year_end' => $value->year_end
];
}
if(!empty($insert)){
DB::table('cars')->insert($insert);
}
}
return redirect()->back()->with('success', true);
}
那是我的表格
<form method="POST" action="uploadedCars" enctype="multipart/form-data" >
Select Excel-Sheet to upload:
<input type="file" name="file" id="file">
<br>
<button class="btn btn-primary" type="submit" name="submit"> Upload Sheet </button>
</form>
有人能帮帮我吗?当我dd()从上传它返回的数据给我什么,我的excel表不是空的?
答案 0 :(得分:0)
替换此行
$path = $file->move(public_path('uploads'), $file);
与
$destinationPath = 'uploads';
$file->move($destinationPath,$file->getClientOriginalName());