简体   繁体   中英

Laravel file upload confusion

So, I am trying to battle the old file upload inside of the Laravel framework but getting a bit lost. I have managed to get the upload to work so the file uploads and saved into an assets folder with a random string name.

This is the form:

<form action="{{ URL::route('account-upload') }}" method="post">
{{ Form::label('file','Upload File') }}
{{ Form::file('file') }}
<br />
{{ Form::submit('Upload') }}
{{ Form::token() }}
</form>

This is the Route:

Route::get('/account/upload', array(
    'as' => 'account-upload',
    'uses' => 'AccountController@getUpload'
));


    Route::post('/account/upload', function(){

        if (Input::hasFile('file')){
            $dest = 'assets/uploads/';
            $name = str_random(6).'_'. Input::file('file')->getClientOriginalName();
            Input::file('file')->move($dest,$name);

            return Redirect::to('/account/upload')
                ->withGlobal('Your image has been uploaded');
        }

    });

this is the method inside AccountController:

public function getUpload(){
    return View::make('account.upload');
}

public function postUpload() {
     $user  = User::find(Auth::id());
     $user->image  = Input::get('file');
}

I am now trying to enable that to push the string name into the database and also be associated with the user who uploaded it and show as their profile image? Ay pointers would be great!

I have created a row inside of the database named 'file' with the type of text....I am not sure on this point of how to store and view the image.

try this

// the view
{{ Form::open(['route' => 'account-upload', 'files' => true]) }}
    {{ Form::label('file','Upload File') }}
    {{ Form::file('file') }}
    <br />
    {{ Form::submit('Upload') }}
{{ Form::close() }}


// route.php
Route::get('/account/upload', 'AccountController@upload');

Route::post('/account/upload', [
    'as'   => 'account-upload',
    'uses' => 'AccountController@store'
]);


// AccountController.php
class AccountController extends BaseController {

    public function upload(){
        return View::make('account.upload');
    }

    public function store() {
        if (Input::hasFile('file')){

            $file = Input::file('file');
            $dest = public_path().'/assets/uploads/';
            $name = str_random(6).'_'. $file->getClientOriginalName();

            $file->move($dest,$name);

            $user      = User::find(Auth::id());
            $user->image  = $name;
            $user->save();

            return Redirect::back()
                ->withGlobal('Your image has been uploaded');
        }
    }
}

// and to display the img on the view
<img src="assets/upload/{{Auth::user()->image}}"/>

In order to upload a file, you'll need enctype="multipart/form-data" as an attribute on the <form> element.

If you're using the Form::open() method, you can just pass "files" => true here, but this should allow you to actually use Input::file() correctly.

Next, when actually dealing with the file, you'll need to use something like storage_path() or public_path() and give an absolute path to the file's destination when moving it.

And a tip: you fetch an authed user's model by calling Auth::user() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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