Laravel 5.2将图像保存到公共文件夹中

时间:2016-05-06 20:45:16

标签: php laravel file-upload laravel-5 laravel-5.2

修改

我正在尝试做一个表单,当你选择并上传图片时。然后我想将此图像保存到我的项目中的公用文件夹(例如:project / public / images)。我收到了这个错误:

ReflectionException in Route.php line 280:
Class App\Http\Controllers\ImageUploadRequest does not exist

我的HomeController:

<?php

namespace App\Http\Controllers;

use App\Article;
use App\Http\Requests;
use App\Http\Requests\ImageUploadRequest;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Input;

class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }
public function uploadImage(ImageUploadRequest $request) {
        if (Auth::check()) {
            if (Auth::user()->admin == 1) {
                $image = $request->file('file');
                //$filename  = time()."-".$image->getClientOriginalName();
                $image->move(public_path("/uploads"), "test");
                //$file = Image::make(sprintf('uploads/%s', "ahoj"))->resize(600,400)->save();
                return view('admin.uploadImage');
            }
            return view('/');
        }
        return view('/');
    }

ImageUploadRequest:

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

abstract class ImageUploadRequest extends FormRequest
{
    public function rules()
    {
        return [
            'file' => 'required'
        ];
    }
}

routes.php文件:

Route::post('uploadImage', [
        'as' => 'uploadImage',
        'uses' => 'ImageUploadRequest@rules'
    ]);

我的表格:

@extends('layouts.app')

@section('content')

<form name="upload_image" method="post" action="{{route('uploadImage')}}" enctype="multipart/form-data">
    <input type="file" accept="image/*" name="file">
    <input type="hidden" name="_token" value="{{ csrf_token() }}">
    <input type="submit" name="submit">
</form>
{{$path = "ahoj"}}
            <p>{{$path}}</p>
@endsection

2 个答案:

答案 0 :(得分:3)

请尝试使用此路线:

Route::post('uploadImage', [
    'as' => 'uploadImage',
    'uses' => 'HomeController@uploadImage'
]);

您正在调用请求而不是控制器。

答案 1 :(得分:0)

您忘记了表格中的enctype="multipart/form-data"