gpt4 book ai didi

php - Laravel 文件上传困惑

转载 作者:行者123 更新时间:2023-12-02 00:24:48 25 4
gpt4 key购买 nike

所以,我试图与 Laravel 框架内的旧文件上传作斗争,但有点迷失了。我已设法使上传工作正常,因此文件上传并保存到具有随机字符串名称的 Assets 文件夹中。

这是表格:

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

这是路线:

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');
}

});

这是AccountController内部的方法:

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

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

我现在正在尝试启用该功能,将字符串名称推送到数据库中,并与上传它的用户关联并显示为他们的个人资料图片?指导一下就太好了!

我在数据库中创建了一行名为"file"的文本类型......我不确定如何存储和查看图像。

最佳答案

试试这个

// 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}}"/>

关于php - Laravel 文件上传困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28268758/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com