首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将文件上传到数据库

将文件上传到数据库
EN

Stack Overflow用户
提问于 2016-12-04 07:01:53
回答 1查看 14.6K关注 0票数 3

,这就是我所拥有的:

名为lamanInformasi的数据库表,其字段名为isi

,这就是我想要的:

用户可以上传多个文件或图像文件,这些文件将被存储到数据库中。文件名将保存到isi字段,文件本身将保存到名为propic的文件夹中。用户还可以在网站上显示他们想要的文件并下载。

,这就是我所做的:

我使用引导插件输入文件。我用了这个来源。我只是使用了jscss文件夹中的文件。这是我的密码:

代码语言:javascript
复制
<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>

,这是我的问题,

如何将文件保存到数据库中?如何在网站上显示文件?如何使文件可下载?谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-12-04 14:47:32

您需要给文件输入字段命名如下

代码语言:javascript
复制
<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>  

然后,在控制器方法中,您可以访问上传的文件并获取各种属性。

代码语言:javascript
复制
**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文档:

数据库查询生成器

雄辩式方法

您可以使用拉勒维尔文件系统从文件夹中检索文件,并为用户准备列表视图,以查看可供下载的文件。

然后使用can利用Laravel的响应下载为用户提供下载功能。

希望这能有所帮助。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40956271

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档