douchui4459 2014-07-28 12:15
浏览 80
已采纳

无法将文件上传到服务器Laravel

I'm trying to add some products to my database and I have to upload photo of this product. I've made a controller and view but when I click Create I've got an error

Unable to create the "/uploads/products/" directory

Here is my code:
Controller

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();
    }



}

View:

{{ Form::open(array('url'=>'user/admin/products/addd', 'files' => true, '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::textarea('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('Add', array('class'=>'btn btn-large btn-primary btn-block'))}}
        {{ Form::close() }}             

Update

Current bootstrap/paths.php file:

return array(       
    'app' => __DIR__.'/../app',
    'public' => __DIR__.'/../public',
    'base' => __DIR__.'/..',
    'storage' => __DIR__.'/../app/storage',  
);
  • 写回答

1条回答 默认 最新

  • duankua3620 2014-07-28 12:18
    关注

    Sounds like the public/uploads/products folder does not exist, and Laravel does not have permission to create it.

    Try create the folder manually, make sure Laravel has writable access to it by running chmod 775 public/uploads/products . Then try the code again.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码