【问题标题】:laravel - file not uploading to uploads folderlaravel - 文件未上传到上传文件夹
【发布时间】:2016-04-25 18:38:20
【问题描述】:

这是我提出的previous 问题的后续问题。我按照说明添加了代码,但是现在当我的表单点击上传字段时,我得到的只是被重定向到表单。

这是我的看法:

@extends('app');

@section('content');
    <h1>Add a new item</h1>
    <hr />
    <content>
        <div class="form-group">
        {!! Form::open(['route' => 'item.store', 'files' => true]) !!}
        {!! Form::label('name', "Name") !!}
        {!! Form::text('name', null, ['class' => 'form-control']) !!}

       {!! Form::label('filename', "File Name") !!}
        {!! Form::file('filename', null, ['class' => 'form-control']) !!}

        {!! Form::label('description', 'Description') !!}
        {!! Form::textarea('description', null, ['class' => 'form-control']) !!}
        {!! Form::submit('Add Item', ['class' => 'btn btn-primary form-control']) !!}

    </content>
</div>

@stop

这是我的控制器

      public function store(Requests\CreateItem $request)
    {


        Item::create($request->all());

//        if (Input::hasFile('filename')) {
//            $file = $request->file('filename');
//            $file->move(public_path().'/uploads', $file->getClientOriginalName());
//
//            echo "File Uploaded";
//
//        }
        dd(Input::all());




    }

这是我的路线

    <?php
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/
Route::resource('item', 'ItemController');

Route::get('/', function() {
    return view('welcome');
});
Route::auth();

Route::get('/home', 'HomeController@index');
Route::post('/item/create', ['as' => 'item.store', 'uses' => 'ItemController@store']);

Route::get('/item', 'ItemController@store');

//Route

有什么建议吗? 编辑:这是我的 Requests\CreateItem.php 文件

    <?php

namespace App\Http\Requests;

use App\Http\Requests\Request;

class CreateItem extends Request
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true; // for now
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'name' => 'required|min:3',
            'filename', 'required|min:7',

        ];
    }
}

【问题讨论】:

  • 一定是Requests\CreateItem 中的某些东西失败了。你能告诉我们一些相关的代码吗?

标签: php file laravel upload


【解决方案1】:

首先我认为你应该删除这一行:

Route::post('/item/create', ['as' =&gt; 'item.store', 'uses' =&gt; 'ItemController@store']);

如果您想更改路由(名称或控制器方法)中的某些想法,则应将其放在此行之前:

Route::resource('item', 'ItemController');

您的路线顺序错误,这就是您返回表单的原因。

【讨论】:

    【解决方案2】:

    我想我可能已经找到了答案。 'filename', 'required|min:7', 应该是 filename =&gt; 'required|min:7'

    【讨论】:

      猜你喜欢
      • 2015-02-27
      • 1970-01-01
      • 1970-01-01
      • 2021-12-18
      • 1970-01-01
      • 2022-11-20
      • 2019-01-27
      • 2018-05-03
      • 1970-01-01
      相关资源
      最近更新 更多