繁体   English   中英

无法在Laravel 4中上传照片

[英]Can't upload photo in Laravel 4

我正在尝试将一些产品添加到我的数据库中,并且我必须上传该产品的照片。 我已经制作了一个控制器并进行了查看,但是当我单击“创建”时,没有任何错误,但是也没有照片。 我只想上传jpg,jpeg,gif,png文件,该怎么办? 这是我的代码:

控制器:

public function postAddProduct(){
    $destinationPath = '';
    $filename        = '';
    $newId = Product::max('id')+1;
    $validator = Validator::make(Input::all(), array(
        'name' => 'required',       
        'description' => 'required',            
        'partner_link' => 'required',
        'image' => 'required'   
    ));
    if (Input::hasFile('image')) {
        $file            = Input::file('image');
        $destinationPath = public_path().'/uploads/products/';
        $filename        = $newId.'.'.$file->getClientOriginalExtension();
        $uploadSuccess   = $file->move($destinationPath, $filename);
    }

    if($validator->passes()){
        $product = new Product;
        $product->name = Input::get('name');
        $product->description = Input::get('description');
        $product->category_id = Input::get('category');
        $product->partner_link = Input::get('partner_link');
        $product->photo = $filename;
        $product->save();   

        return Redirect::back();
    }else{
        return Redirect::back()->withErrors($validator)->withInput();
    }

}

视图:

{{ Form::open(array('url'=>'user/admin/products/addd', 'class'=>'col-md-4', 'style'=> 'float:none; margin: 0 auto', 'id'=>'register-form')) }}
            <h2 class="form-signin-heading">Add Product</h2>    
            {{ Form::text('name', null, array('class'=>'form-control', 'placeholder'=>'Name')) }}    
            {{ Form::text('description', null, array('class'=>'form-control', 'placeholder'=>'Description')) }}
            {{ Form::text('partner_link', null, array('class'=>'form-control', 'placeholder'=>'Partner link')) }}
            {{Form::label('category', 'Category: ', array('class' => 'field-name'))}}
            <select name="category">
                <?php $i = 0; ?>
                @foreach($categories as $category)      
                    <optgroup label="{{$category['name']}}">
                    @foreach($category['subcategories'] as $sub)
                        <option value="{{$sub->id}}">{{$sub->name}}</option>
                    @endforeach
                    </optgroup>
                @endforeach
            </select>
            <div class="clearfix"></div>                
            {{Form::file('image', array('style' => 'margin-bottom: 10px'))}}
            {{ Form::submit('Save', array('class'=>'btn btn-large btn-primary btn-block'))}}
        {{ Form::close() }} 

您的表单应将选项“文件”设置为“真”:

{{ Form::open(array('url' => 'foo/bar', 'files' => true)) }}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM