在本主题中,我们将了解如何上传文件。
让我们通过一个例子来理解。
作曲家创建项目laravel / laravel = 5.8 form -prefer-dist;
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateFormsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('forms', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('path');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('forms');
}
}
上面的代码创建了一个名为“ forms ”的表,该表包含四列(id,path,created_at,updated_at)。
form.blade.php
<Html>
<Head>
<title> File Upload </title>
</Head>
<Body>
<form method="Post" action="{{route('forms.store')}}" enctype="multipart/form-data">
@csrf
<div><input type="file" name="image"> </div><br/>
<div><button type="submit">Upload </button></div>
</form>
</body>
在此,我们将定义store()函数,在其中添加将文件保存在数据库中的代码。
FormController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Form;
class FormController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
$data=new Form;
if($files=$request->file('image')){
$name=$files->getClientOriginalName();
$files->move('images',$name);
$data->path=$name;
}
$data->save();
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
在上面的代码中,我们定义了store()函数,将文件存储在$ name变量中,然后将文件移动到images文件夹。通过使用语句$ data-> save() ,将我们移至图像文件夹的文件保存在数据库中。
Route::get('/file-upload', function () {
return view('form');
});
输出量
单击“选择文件”按钮时,我们需要选择要上传的文件。假设我选择了以下屏幕快照中所示的名为“ img.png”的文件:
在上面的屏幕截图中,我选择了文件( img.png ),然后单击“上传”按钮。单击“上传”按钮后,文件将保存到数据库,如以下屏幕快照所示:
在本节中,我们将看到如何从数据库中检索数据。
FormController.php
public function index()
{
$cruds = Crud::all();
return view('index', compact('cruds'));
}
index.blade.php
@extends('layout.master')
@section('content')
@foreach($forms as $form)
<div>
<img src="./images/{{ $form->path}}">
</div>
@endforeach
在上面的代码中,“ ./images/{{$form->path}} ”定义了图像的路径,即,图像存储在images文件夹中。
路线:: get('/ show','FormController @ index');
输出量
在上述情况下,我们通过将路径传递给src属性来使用静态方式显示图像。我们还可以显示图像而无需在<img>标记中传递文件夹的名称(图像),这可以通过在Form模型中定义getPathAttribute()函数来实现。
Form.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Form extends Model
{
//
public $directory="./images/";
protected $table='forms';
public function getPathAttribute($value)
{
return $this->directory.$value;
}
}
index.blade.php
@extends('layout.master')
@section('content')
@foreach($forms as $form)
<div>
<img src="{{ $form->path}}">
</div>
@endforeach
输出量
Made with ❤️ in Chengdu. Copyright reserved 2019-2022.
蜀ICP备20006366号-1