我试着用Laravel制作多幅图像,但是我收到了
'为foreach()提供的参数无效'
这是控制器的功能
public function uploadSubmit() {
$files = Input::file('image');
$file_count = count($files);
$gallery = 0;
foreach($files as $file) {
$gallery = new Gallery;
$rules = array('file' => 'required');
$validator = Validator::make(array('file'=> $file), $rules);
if($validator->passes()){
$filename = str_random(20);
$file->move(public_path() . '/uploads', $filename . '.' . $file->getClientOriginalExtension());
$imagePath = '/uploads/' . $filename . '.' . $file->getClientOriginalExtension();
$image = Image::make(public_path() . $imagePath);
$image->save();
$gallery ++;
$gallery->image = $imagePath;
$gallery->save();
}
}
if($gallery == $file_count){
return Redirect::to('/admin/upload')->with('message', 'image added.');
}
else
{
return Redirect::to('/admin/upload')->withInput()->withErrors($validator);
}
}
当我
var_dump($files);
时,它返回NULL
。形式是
{{ Form::open() }}
{{ Form::file('image[]', array('multiple'=>true)) }}
<hr />
<button type="submit" class="btn btn-primary">upload</button>
{{ Form::close() }}
我的路线:
Route::get ('/admin/upload', ['uses' => 'AdminController@upload', 'before' => 'admin']);
Route::post('/admin/upload', ['uses' => 'AdminController@uploadSubmit', 'before' => 'csrf|admin']);
最佳答案
创建表单时使文件为真
{!!Form::打开(array('route'=>'image.upload','method'=>'POST',
'文件'=>真)!!}
{!! Form::open(array('route' => 'image.upload', 'method' => 'POST', 'files' => true)) !!}
{{ Form::file('image[]', array('multiple'=>true)) }}
<hr />
<button type="submit" class="btn btn-primary">upload</button>
{{ Form::close() }}
你的职能
public function uploadSubmit() {
$files = Input::file('image');
$file_count = count($files);
foreach($files as $file) {
$gallery = new Gallery;
$rules = array('file' => 'required');
$validator = Validator::make(array('file'=> $file), $rules);
if($validator->passes()){
$filename = str_random(20). '.' . $file->getClientOriginalExtension();
$file->move('uploads', $filename );
$gallery->image = $filename;
$gallery->save();
}
}
}
关于php - 使用Laravel上传多张图片,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40357195/