BinarySoul's avatar

Wierd result when trying to make an working file upload in Laravel 5

Hello,

i try to make a upload dialog for images. But the result is:

  1. i choose a file
  2. i click my upload Button
  3. it says: Success! (but it havent done anything.)

here is my Code:

views/movies.blade.php:

@extends('layouts.decadance_main')
@section('sitetitle')
    Movielist
@endsection
@section('content')
    <div class="about-section">
        <div class="text-content">
            <div class="span7 offset1">
                @if(Session::has('success'))
                    <div class="alert-box success">
                        <h2>{!! Session::get('success') !!}</h2>
                    </div>
                @endif
                <div class="secure">Upload form</div>
                {!! Form::open(array('url'=>'movies','method'=>'POST', 'files'=>true)) !!}
                <div class="control-group">
                    <div class="controls">
                        {!! Form::file('image') !!}
                        <p class="errors">{!!$errors->first('image')!!}</p>
                        @if(Session::has('error'))
                            <p class="errors">{!! Session::get('error') !!}</p>
                        @endif
                    </div>
                </div>
                <div id="success"> </div>
                {!! Form::submit('Submit', array('class'=>'send-btn')) !!}
                {!! Form::close() !!}
            </div>
        </div>
    </div>
@endsection

Controllers/UploadController.php:

<?php namespace App\Http\Controllers;
use Illuminate\Support\Facades\Input;
use Validator;
use Redirect;
use Request;
use Session;
class UploadController extends Controller {

    public function upload() {
        $file = array('image' => Input::file('image'));
        $rules = array('image' => 'required',); 
        $validator = Validator::make($file, $rules);
        if ($validator->fails()) {
            return Redirect::to('movies')->withInput()->withErrors($validator);
        }
        else {
            if (Input::file('image')->isValid()) {
                $destinationPath = 'public.uploads'; 
                $extension = Input::file('image')->getClientOriginalExtension();
                $fileName = rand(11111,99999).'.'.$extension;
                Input::file('image')->move($destinationPath, $fileName);
                Session::flash('success', 'Upload successfully');
                return Redirect::to('movies');
            }
            else {
                Session::flash('error', 'uploaded file is not valid');
                return Redirect::to('movies');
            }
        }
    }
}

and finaly, my routes.php:

<?php



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

Route::auth();

Route::get('/home', 'PublicController@nav2index');
Route::get('/movies', 'PrivateController@nav2movies');
Route::post('movies', 'UploadController@upload');

Hope someone can tell me, where to find the error... Thank you guys.

0 likes
3 replies
SaeedPrez's avatar
Level 50

@BinarySoul I've refactored your controller code to make it more readable and tested it on Laravel 5.2..

class UploadController extends Controller
{

    public function upload(Request $request)
    {
        // Validate input
        $validator = Validator::make($request->all(), [
            'image' => 'required|image',
        ]);

        // If validation fails, redirect back with error messages
        if ($validator->fails()) {
            return back()->withInput()->withErrors($validator);
        }

        // Set upload destination path and generate file name
        $destinationPath = 'uploads';
        $fileName = rand(11111, 99999) . '.' . $request->file('image')->getClientOriginalExtension();

        // Move the uploaded file
        $request->file('image')->move($destinationPath, $fileName);

        // Hey, we made it! Flash success message
        session()->flash('success', 'Upload successfully');

        return redirect()->to('movies');
    }
}
2 likes

Please or to participate in this conversation.