如何通过我的数据库保存文件名/图像?

时间:2016-11-29 02:05:28

标签: php laravel file-upload frameworks

我正在进行文件上传。我通过互联网搜索了文件上传。我看到的网站只保存在images文件夹中,我试图把Administrator :: create([' image' => $ request-> image]); 它保存到数据库但它不是我上传的图像的名称..上传后的图像名称C:\ xampp \ tmp \ php306A.tmp

我的控制器

<?php 

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Administrator;
class ImageController extends Controller
{

    /**
    * Create view file
    *
    * @return void
    */
    public function imageUpload()
    {
        return view('image-upload');
    }

    /**
    * Manage Post Request
    *
    * @return void
    */
    public function imageUploadPost(Request $request)
    {
        $file = $request->file('image');
        $this->validate($request, [
            'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
        ]);

        $imageName = time().'.'.$request->image->getClientOriginalExtension();
        $request->image->move(public_path('images'), $imageName);
        // Administrator::create($request->all());
        Administrator::create([
            'image' => $request->image 
            ]);

        return back()
            ->with('success','Image Uploaded successfully.')
            ->with('path',$imageName);
    }

}
?>

我的观点

<!DOCTYPE html>
<html>
<head>
    <title>Laravel 5.3 Image Upload with Validation example</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
</head>
<body>

<div class="container">
<div class="panel panel-primary">
  <div class="panel-heading"><h2>Laravel 5.3 Image Upload with Validation example</h2></div>
  <div class="panel-body">

        @if (count($errors) > 0)
            <div class="alert alert-danger">
                <strong>Whoops!</strong> There were some problems with your input.<br><br>
                <ul>
                    @foreach ($errors->all() as $error)
                        <li>{{ $error }}</li>
                    @endforeach
                </ul>
            </div>
        @endif

        @if ($message = Session::get('success'))
        <div class="alert alert-success alert-block">
            <button type="button" class="close" data-dismiss="alert">×</button>
                <strong>{{ $message }}</strong>
        </div>
        <img src="/images/{{ Session::get('path') }}">
        @endif

        <form action="{{ url('image-upload') }}" enctype="multipart/form-data" method="POST">
            {{ csrf_field() }}
            <div class="row">
                <div class="col-md-12">
                    <input type="file" name="image" />
                </div>
                <div class="col-md-12">
                    <button type="submit" class="btn btn-success">Upload</button>
                </div>
            </div>
        </form>

  </div>
</div>

</div>

</body>
</html>

我的路线

Route::get('image-upload','ImageController@imageUpload');
Route::post('image-upload','ImageController@imageUploadPost');

我的模特

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Administrator extends Model
{
    protected $table = 'accounts';
    protected $fillable = array(
        'first_name',
        'middle_name',
        'last_name',    
        'email',
        'image',    
        ''
        );

}

2 个答案:

答案 0 :(得分:0)

要获取上传的图片,您可以更改这些行:

$imageName = time().'.'.$request->image->getClientOriginalExtension();
$request->image->move(public_path('images'), $imageName);

$imageName = "";
if($request->hasFile('images')) {
    $image = $request->file('images');
    $imageName = time() . '.' . $image->getClientOriginalExtension();
    $image->move(public_path('images'), $imageName);
}

你的创作部分:

Administrator::create([
    'image' => $imageName 
]);

答案 1 :(得分:0)

查看部分

 {!! Form::open(array('url'=>'insertfile','method'=>'POST' ,'class'=>'form-horizontal','files'=>true)) !!}

  <input type="hidden" name="_token" value="{{ csrf_token() }}">

    <div class="form-group">
      <label class="control-label col-sm-2" for="pwd">Upload:</label>

      <div class="col-sm-4">          

        <input type="file"  name="filenam" class="filename">
      </div>
    </div>

    <div class="form-group">        
      <div class="col-sm-offset-2 col-sm-10">
        <button type="submit" class="btn btn-default">Submit</button>
      </div>
    </div>
{!! Form::close() !!}

<强>控制器

public function insertFile(){

    $file= Input::file('filenam');

    $rules = array('file' => 'required|max:10000|mimes:jpeg,png,jpg,gif,svg,csv,xls,xlsx,doc,docx,pdf');

    $validator = Validator::make(array('file'=> $file), $rules);

    if($validator->passes()){


    $destinationPath = 'up_file';

    $filename = $file->getClientOriginalName();

            $data=array(

                'file_name' => $filename,
            );

            var_dump($data);

          yourmodelname::insert($data);


    $upload_success = $file->move($destinationPath, $filename);



  }

}

路线:

Route::get('/uploadfile','UploadController@getView');
Route::post('/insertfile','UploadController@insertFile');

文件将上传到 yourproject / public / up_file目录