请帮我创建一个使用Laravel 5.4的图像上传系统,也可以将文件名保存在数据库中...
我无法找到有关此问题的相关文章,我也尝试了一个youtube教程,但它没有解释如何在数据库上传输文件名,希望你能帮助我 谢谢......
到目前为止我的代码已经完成了......
$this->validate(request(), [
'article_banner' => 'required | mimes:jpeg,jpg,png | max:2000',
'article_title' => 'required|max:255',
'article_date' => 'required|date',
'article_content' => 'required',
]
);
$article_banner = $request->file('article_banner');
$article_title = $request->input('article_title');
$article_date = $request->input('article_date');
$article_content = $request->input('article_content');
return $article_banner;
}
这里是article_add.php
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">User Management -> Edit User</div>
<div class="panel-body">
<form class="form-horizontal" role="form" method="POST" action="{{ route('article_add.post') }}" enctype="multipart/form-data">
{{ csrf_field() }}
<div class="form-group{{ $errors->has('article_banner') ? ' has-error' : '' }}">
<label for="article_banner" class="col-md-4 control-label">Article Banner: </label>
<div class="col-md-6">
<input id="article_banner" type="file" class="form-control" name="article_banner" required autofocus>
<p class="help-block">Example block-level help text here.</p>
@if ($errors->has('article_banner'))
<span class="help-block">
<strong>{{ $errors->first('article_banner') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group{{ $errors->has('article_title') ? ' has-error' : '' }}">
<label for="article_title" class="col-md-4 control-label">Article Title: </label>
<div class="col-md-6">
<input id="article_title" type="text" class="form-control" name="article_title" value="{{ old('article_title') }}" required autofocus>
@if ($errors->has('article_title'))
<span class="help-block">
<strong>{{ $errors->first('article_title') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group{{ $errors->has('article_date') ? ' has-error' : '' }}">
<label for="article_date" class="col-md-4 control-label">Article Date: </label>
<div class="col-md-6">
<input id="article_date datepicker" type="text" class="form-control datepicker" name="article_date" value="{{ old('article_date') }}" data-provide="datepicker" required autofocus>
@if ($errors->has('article_date'))
<span class="help-block">
<strong>{{ $errors->first('article_date') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group{{ $errors->has('article_content') ? ' has-error' : '' }}">
<div style="padding:10px;">
<label for="article_content">Article Date: </label>
<br />
<textarea id="content article_content" type="text" class="form-control" name="article_content" autofocus>{{ old('article_content') }}</textarea>
</div>
@if ($errors->has('article_content'))
<span class="help-block">
<strong>{{ $errors->first('article_content') }}</strong>
</span>
@endif
</div>
@if(session()->has('message'))
<div class="alert alert-success">
{{ session()->get('message') }}
</div>
@endif
@if(session()->has('errors'))
<div class="alert alert-danger">
{{ session()->get('errors') }}
</div>
@endif
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary">
Submit
</button>
<a href="{{ url('article_management') }}" class="btn btn-primary">
Back
</a>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection
答案 0 :(得分:0)
将一个功能设为
public function uploadFiles($_destination_path, $images, $new_file_name) { //code to uplaod multiple fiels to path and return paths array wit file names
$file_name = str_replace(' ', '-', $new_file_name);
$paths = array('path' => $_destination_path . '/' . basename(Storage::disk($this->diskStorage)->putFileAs($_destination_path, $images, $file_name)),
'name' => pathinfo($file_name));
return $paths;
}
并将所需的参数传递给它,如
$image = $request->file('image');
$fileName = $image->getClientOriginalName();
$destinationPath = '/images';
$img_path[] = $this->uploadFiles($destinationPath, $image, $fileName);
您将在img_path []数组变量中获取所需数据。
答案 1 :(得分:0)
public function feedbackPost(Request $request, $id)
{
$fileName1 = "";
$fileName2 = "";
$rules = array(
'conferencename' =>'required',
'yourname' =>'required',
'email' =>'required',
'objective' =>'required',
'results' =>'required',
'recommendations' =>'required',
'key_customers' =>'required',
'actions' =>'required',
'business_opportunities' =>'required',
'other_opportunities' =>'required',
'upload_leads' =>'mimes:csv,xls,xlsx',
'upload_attendees' =>'mimes:csv,xls,xlsx',
);
$validator = Validator::make($request->all(), $rules);
if ($validator->fails())
{
return back()->with('danger', 'File format not valid');
}
else
{
if($file=$request->hasFile('upload_attendees')) {
$file=$request->file('upload_attendees');
$fileName1=$file->getClientOriginalName();
if (!file_exists('uploads/feedback/attendees/'.$id.'')) {
mkdir('uploads/feedback/attendees/'.$id.'', 0777, true);
}
$destinationPath='uploads/feedback/attendees/'.$id.'';
$file->move($destinationPath,$fileName1);
}
if($file=$request->hasFile('upload_leads')) {
$file=$request->file('upload_leads');
$fileName2=$file->getClientOriginalName();
if (!file_exists('uploads/feedback/leads/'.$id.'')) {
mkdir('uploads/feedback/leads/'.$id.'', 0777, true);
}
$destinationPath='uploads/feedback/leads/'.$id.'';
$file->move($destinationPath,$fileName2);
}
$feedback = Feedback::insert([
'user_id' => $request->user_id,
'conferenceid' => $request->conferenceid,
'conferencename' =>$request->conferencename,
'yourname' =>$request->yourname,
'email' =>$request->email,
'objective' =>$request->objective,
'results' =>$request->results,
'recommendations' =>$request->recommendations,
'key_customers' =>$request->key_customers,
'actions' =>$request->actions,
'business_opportunities' =>$request->business_opportunities,
'other_opportunities' =>$request->other_opportunities,
'upload_attendees' =>$fileName1,
'upload_leads' =>$fileName2,
]);
}
return back()->with('success', 'Thanks! Your Feedback has been Submitted!');
}
这就是我的方式。你可以试试这个。