首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用Laravel上传的多幅图像

用Laravel上传的多幅图像
EN

Stack Overflow用户
提问于 2016-11-01 09:24:20
回答 1查看 477关注 0票数 1

我试着用Laravel制作多个图像功能,但我已经收到了

“为foreach()提供的无效参数”

这是控制器中的函数。

代码语言:javascript
运行
复制
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

表格是

代码语言:javascript
运行
复制
{{ Form::open() }}
    {{ Form::file('image[]', array('multiple'=>true)) }}
        <hr />
        <button type="submit" class="btn btn-primary">upload</button>           
{{ Form::close() }}

我的路线:

代码语言:javascript
运行
复制
Route::get ('/admin/upload', ['uses' => 'AdminController@upload', 'before' => 'admin']);
Route::post('/admin/upload', ['uses' => 'AdminController@uploadSubmit', 'before' => 'csrf|admin']);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-11-01 09:27:49

创建窗体时使文件变为真

{!形式:打开(数组(‘路由’=>‘图片.上载’,‘方法’=> 'POST',‘文件’=>真)!}

代码语言:javascript
运行
复制
{!! 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() }}

你的职能

代码语言:javascript
运行
复制
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();
        }
   }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40357195

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档