2016-11-29 55 views
0

我正在做文件上传。我通过互联网搜索文件上传。我看到的网站只是通过图像文件夹保存,我试图把管理员::创建(['image'=> $ request-> image]); 它保存到数据库中,但它不是我上传的图片的名称..图像后,我上传下的名称:\ 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','[email protected]'); 
Route::post('image-upload','[email protected]'); 

我的模型

<?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',  
     '' 
     ); 

} 
+0

的可能的复制【如何存储和使用Laravel数据库中检索图像内容(http://stackoverflow.com/questions/35428876/如何存储和检索图像内容从数据库使用laravel) – Ronald

+0

先生,我reedit我的帖子 – Angel

回答

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 
]); 
+0

Oohh nicee,谢谢你..但是,如果用户想上传文件,csv或Excel文件?我应该如何允许它? – Angel

+0

然后您应该更改MIME类型验证,例如: 'image'=>'必需| mimes:jpeg,png,jpg,gif,svg,csv,xls,xlsx,doc,docx | max:2048' – AWJ

+0

已经尝试过但它不工作,为什么? – Angel

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() !!} 

控制器

公共功能的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','[email protected]'); 
Route::post('/insertfile','[email protected]'); 

文件将在yourproject /公/ up_file目录下上传

+0

我试过你的工作,我的问题是也一样..它不能上传其他文件(doc,docx等等)。 – Angel

+0

但这段代码适用于doc pdf jpg png我测试过 – Borna

+0

你是否使用files = true形式?????? – Borna

相关问题