2016-12-04 112 views
1

這些是我有:命名爲lamanInformasiLaravel - 上傳文件到數據庫

數據庫中的表,其中有場名爲isi

這就是我想要的東西:

用戶可以上傳多個文檔或圖像文件,並將這些文件將被存儲到數據庫中。文件名將被保存到isi字段,並且文件本身將被保存到名爲propic的文件夾中。用戶也可以在網站上顯示他們想要的文件並下載。

這是我做了什麼:

我用來引導插件來輸入文件。我用這個source。我只是使用jscss文件夾中的文件。這是我的代碼:

<html lang="en"> 
<head> 
    <meta charset="UTF-8"/> 
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet"> 
    <link href="../css/fileinput.css" media="all" rel="stylesheet" type="text/css" /> 
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"> 

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
    <script src="../js/fileinput.js" type="text/javascript"></script> 
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js" type="text/javascript"></script> 
</head> 
<body> 
    <div class="container kv-main"> 
     <div class="page-header"> 
     <h1>Upload Files</h1> 
     </div> 

     <form enctype="multipart/form-data"> 
      <div class="form-group"> 
       <input id="file-5" class="file" type="file" multiple data-preview-file-type="any" data-upload-url="#"> 
      </div> 
     </form> 
    </div> 
</body> 
<script> 
     $("#file-5").fileinput({ 
      uploadUrl: "{{ url('lamanInformasi') }}", 
      uploadAsync: false, 
      previewFileIcon: '<i class="fa fa-file"></i>', 
      allowedPreviewTypes: 'image', 
      previewFileIconSettings: { 
       'doc': '<i class="fa fa-file-word-o text-primary"></i>', 
       'xls': '<i class="fa fa-file-excel-o text-success"></i>', 
       'ppt': '<i class="fa fa-file-powerpoint-o text-danger"></i>', 
       'jpg': '<i class="fa fa-file-photo-o text-warning"></i>', 
       'pdf': '<i class="fa fa-file-pdf-o text-danger"></i>', 
       'zip': '<i class="fa fa-file-archive-o text-muted"></i>', 
       'htm': '<i class="fa fa-file-code-o text-info"></i>', 
       'txt': '<i class="fa fa-file-text-o text-info"></i>', 
       'mov': '<i class="fa fa-file-movie-o text-warning"></i>', 
       'mp3': '<i class="fa fa-file-audio-o text-warning"></i>', 
      }, 
      previewFileExtSettings: { 
       'doc': function(ext) { 
        return ext.match(/(doc|docx)$/i); 
       }, 
       'xls': function(ext) { 
        return ext.match(/(xls|xlsx)$/i); 
       }, 
       'ppt': function(ext) { 
        return ext.match(/(ppt|pptx)$/i); 
       }, 
       'zip': function(ext) { 
        return ext.match(/(zip|rar|tar|gzip|gz|7z)$/i); 
       }, 
       'htm': function(ext) { 
        return ext.match(/(php|js|css|htm|html)$/i); 
       }, 
       'txt': function(ext) { 
        return ext.match(/(txt|ini|md)$/i); 
       }, 
       'mov': function(ext) { 
        return ext.match(/(avi|mpg|mkv|mov|mp4|3gp|webm|wmv)$/i); 
       }, 
       'mp3': function(ext) { 
        return ext.match(/(mp3|wav)$/i); 
       }, 
      } 
     }); 
</script> 

這是我的問題

如何將文件保存到數據庫?如何在網站上顯示文件?如何使文件變得可下載?由於

回答

1

你需要給文件輸入域的名稱,如

<form enctype="multipart/form-data"> 
     <div class="form-group"> 
      <input id="file-5" name="image" class="file" type="file" multiple data-preview-file-type="any" data-upload-url="#"> 
     </div> 
</form> 

然後在您的控制器的方法,你可以訪問上傳的文件,並獲得各種性能

**EDIT** 
$destinationPath = storage_path().'/uploads/'; 
or 
$destinationPath = public_path().'/uploads/'; 

//storage_path will give the fully qualified path to the storage folder 
//public_path will give the fully qualified path to the public folder 
//uploads - is the folder name where you want to store the user uploaded files, could be any name you prefer. 
**EDIT end** 

// Retrieving An Uploaded File 
$file = Input::file('image'); 

// Determining If A File Was Uploaded 
if (Input::hasFile('image')) 
{ 
    // 
} 

// Determining If An Uploaded File Is Valid 
if (Input::file('image')->isValid()) 
{ 
    // 
} 

// Moving An Uploaded File 
Input::file('image')->move($destinationPath); 
Input::file('image')->move($destinationPath, $fileName); //$filename is the name by which you want to store the file - change the name if required by by app. 

// Getting Requested file path 
$path = Input::file('image')->getRealPath(); 

// Getting Original name of the file 
//**Edit** - gives the original filename as on uploading user's system. **Edit end** 
$name = Input::file('image')->getClientOriginalName(); 

// Getting uploaded File extention 
$extension = Input::file('image')->getClientOriginalExtension(); 

// Getting Size of the file 
$size = Input::file('image')->getSize(); 

// Getting MIME Type of uploaded file 
$mime = Input::file('image')->getMimeType(); 

然後,您可以儲存數據庫中的屬性或者通過Model對應於您的案例lamanInformasi中的數據庫表,或者使用查詢構建器直接將數據庫表與原始查詢結合使用。

看一看laravel文檔:
Database - query builder
Eloquent - model approach

您可以使用Laravel's filesystem從文件夾中檢索文件,並準備一個列表視圖,爲用戶看到可供下載的文件。

然後使用可以利用Laravel's Response Download爲用戶提供下載功能。

希望這會有所幫助。

+0

那麼,在