我正在尝试上传图片:
查看(部分):
<input type="file" name="image" />
Countoller:
public function store(Request $request){
dump($request->all());
$this->validate($request,[
'title'=>'required|max:255',
// 'image' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048',
'text'=>'required',
]);
$imageName = time().'.'.$request->image->getClientOriginalExtension();
$request->image->move(public_path('images'), $imageName);
dump($request);
$data=$request->all();
dump($data);
$aticle=new Article;
$aticle->fill($data);
}
转储请求:
"title" => "fgdfd"
"alias" => "dg"
"desc" => "fdgfgd"
"text" => "gd"
"image" => "IMG_0002.JPG"
"tag" => "hg"
如何将图像放入MySql数据库?
答案 0 :(得分:1)
As the docs describe,您应该使用file()
上的$request
方法来访问上传的文件,而不是文件字段的名称。
在您的情况下,这意味着:
// Use the file() method to access the uploaded file
$imageName = time() . '.' . $request->file('image')->getClientOriginalExtension();
// storeAs() allows you to move a file while specifying a new filename.
// $path will be the fully qualified path to the file, including filename.
$path = $request->file('image')->storeAs(public_path('images'), $imageName);
从您的问题中不清楚您是要将数据库中的文件路径还是实际的二进制文件内容保存为BLOB。以下是两种方法:
// Create new article
$aticle=new Article;
$aticle->fill($data);
// Either save the path to the uploaded image in the DB
$aticle->featured_image = $path;
$aticle->save();
// OR
// Save the file binary contents in the DB
$aticle->featured_image = file_get_contents($path);
$aticle->save();
答案 1 :(得分:0)
理想情况下,您将文件保存到某个位置,然后将该文件的路径存储在数据库中。
你在哪个版本的Laravel?如果你在5.3或更高,你可以:
$path = $request->image->store('path/to/save/the/file');
这将使用随机名称保存文件,然后将该路径存储到数据库中。
或者你可以:
$path = $request->image->storeAs('path/to/save/the/file', 'filename.jpg');
如果要指定保存的文件名。
答案 2 :(得分:0)
这项工作是给我的:
第1步
使用干预图像包来处理图像。您将通过composer安装它。运行命令:
composer require intervention/image
第2步
在安装Intervention Image之后,您需要添加服务提供商和外观。打开我们的config / app.php文件并添加以下行。在$ providers数组中,添加此程序包的服务提供商。
Intervention\Image\ImageServiceProvider::class
第3步
将此包的外观添加到$ aliases数组中。
'Image' => Intervention\Image\Facades\Image::class
第4步
最后使用以下命令发布配置:
$ php artisan vendor:publish --provider="Intervention\Image\ImageServiceProviderLaravel5"
第5步控制器
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
use App\yourmodel;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Input;
use Response;
use Image;
class YourController extends Controller
{
public function create()
{
return view('yourview.create');
}
public function store(Request $request)
{
$file = Input::file('pic');
$img = Image::make($file);
Response::make($img->encode('jpeg'));
$var = new YourModel;
$var->pic = $img;
$var->save();
return redirect()->route('yourview.index')
->with('success','Success.');
}
第6步查看
<form action="{{ route('yourview.store') }}" method="POST" enctype="multipart/form-data">
@csrf
<div class="row">
<div class="col-xs-4 col-sm-4 col-md-4">
<div class="form-group">
<strong>Image:</strong>
<input type="file" id="pic" name="pic" class="form-control"></input>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12 text-center">
<button type="submit" class="btn btn-primary">Send</button>
</div>
</div>
</form>