简体   繁体   English

Laravel文件上传混乱

[英]Laravel file upload confusion

So, I am trying to battle the old file upload inside of the Laravel framework but getting a bit lost. 因此,我试图与Laravel框架中的旧文件上传进行斗争,但是有点迷路。 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: 这是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. 我已经在数据库内部创建了一个名为'file'的文本类型的行。...在这一点上我不确定如何存储和查看图像。

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. 为了上传文件,您需要enctype="multipart/form-data"作为<form>元素上的属性。

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. 如果您使用的是Form::open()方法,则可以在此处传递"files" => true ,但这应允许您正确使用Input::file()

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. 接下来,在实际处理文件时,您需要使用诸如storage_path()public_path()并在移动文件时提供指向文件目的​​地的绝对路径。

And a tip: you fetch an authed user's model by calling Auth::user() . 提示:您可以通过调用Auth::user()获取已认证用户的模型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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