我试图通过在控制器中执行此操作在数据库中上传多个图像:
公共功能商店(请求$ request) {
$pictures=[];
$input=$request->all();
if($file=$request->file('images')) $pictures[]=$request->file('images');
if($file=$request->file('image1')) $pictures[]=$request->file('image1');
if($file=$request->file('image2')) $pictures[]=$request->file('image2');
if($file=$request->file('image3')) $pictures[]=$request->file('image3');
if($file=$request->file('image4')) $pictures[]=$request->file('image4');
if($file=$request->file('image5')) $pictures[]=$request->file('image5');
if($file=$request->file('image6')) $pictures[]=$request->file('image6');
foreach($pictures as $file)
for($name=0;$name<=7;$name++)
{
$name=$file->getClientOriginalName();
}
$file->move('image',$name);
$input['images']=$name;
$input['image1']=$name;
$input['image2']=$name;
$input['image3']=$name;
$input['image4']=$name;
$input['image5']=$name;
$input['image6']=$name;
Detail::create($input);
return redirect('/');
}
它采用图像形式表格并将所选图像存储在公共/图像文件夹中,但在数据库中它存储具有相同图像名称的所有图像。并且在显示时也会多次显示相同的图像。
我知道这里的人有解决方案,可能有更好的主意。所以请帮我解决这个问题。提前谢谢。
以上完成的方法不适合我,所以我在我的控制器中做了这个
public function uploadSubmit(request $request)
{
// Coming soon...
$data=$request->all();
$imagename =[];
$i = 0;
$files =Input::file('images');
foreach($files as $file){
$extension = $file->getClientOriginalExtension();
$imagename[$i] = 'post'.str_random(10).'.jpg';
$destinationPath = 'assets/posts';
$file->move($destinationPath, $imagename[$i]);
$i++;
}
Detail::create($files);
return redirect('/');
}
在途中:
Route::resource('/details','DetailController');
现在我收到如下错误:DetailController.php第43行中的FatalThrowableError: 在数组上调用成员函数getClientOriginalName()。 任何人都可以在这里指出问题。感谢。
答案 0 :(得分:1)
所以我会设置一个自定义请求来验证上传是图像,然后您可以直接在控制器中上传它们,如下面的示例所示。
应用\ HTTP \请强>
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class ImageRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
// Logic to authorize the request
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
$rules = [
// Any other required laravel validations
];
if (is_array($this->request->get('images'))):
foreach ($this->request->get('images') as $key => $val):
if($key == 0) continue;
$rules['images.' . $key] = 'required|image';
endforeach;
endif;
return $rules;
}
}
应用\ HTTP \控制器\ ImageController 强>
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests\ImageRequest;
class ImageController extends Controller
{
public function store(ImageRequest $request) {
$uploadedImagesPath = array();
foreach($request->input("images") as $key => $image):
$thisFile = $request->file("images")[$key];
if($thisFile->isValid()):
// Get original file name
$imageName = $thisFile->getClientOriginalName();
// Upload file to default Laravel configured upload location
$uploadedImagesPath[$key] = $thisFile->storeAs('images', $imageName);
// Function was in your example code
// Detail::create($request->file("images"));
endif;
endforeach;
return redirect('/');
}
}
您可以在Laravel的网站上阅读有关该主题的更多信息
https://laravel.com/docs/5.4/requests#retrieving-uploaded-files
https://laravel.com/docs/5.4/validation#form-request-validation