javascript - 图片从上传文件夹插入数据库

标签 javascript php mysql

我创建了一个表单,它从用户上传图像并存储到我创建的文件夹中,现在我想将图像也插入到 mysql 数据库中,然后也获取它并显示在表中

    function bs_input_file() {
        $(".input-file").before(
                function () {
                    if (!$(this).prev().hasClass('input-ghost')) {
                        var element = $("<input type='file' class='input-ghost' accept='image/jpg,image/jpeg,image/png,image/gif' style='visibility:hidden; height:0'>");
                        element.attr("name", $(this).attr("name"));
                        element.change(function () {
                            element.next(element).find('input').val((element.val()).split('\\').pop());
                        });
                        $(this).find("button.btn-choose").click(function () {
                            element.click();
                        });
                        $(this).find("button.btn-reset").click(function () {
                            element.val(null);
                            $(this).parents(".input-file").find('input').val('');
                        });
                        $(this).find('input').css("cursor", "pointer");
                        $(this).find('input').mousedown(function () {
                            $(this).parents('.input-file').prev().click();
                            return false;
                        });
                        return element;
                    }
                }
        );
    }
    
    function generateAlert(type, message){
        new PNotify({
            text: message,
            type: type,
            title: type,
            hide: true,
            buttons: {
                closer_hover: false,
                sticker: false
            },
            mobile: {
                swipe_dismiss: true,
                styling: true
            }
        });
    }
    
    $(function () {
        bs_input_file();
        
        //upload file code starts here
        $("body").on("click", ".uploadfilebutton", function(e){
            e.preventDefault();
            if ($('.input-ghost').get(0).files.length === 0) {
                alert("No file selected.");
            }
            
            var formData = new FormData();
            formData.append('upload', 'fileupload');
            // Attach file
            formData.append('image', $('.input-ghost')[0].files[0]);
            
            $.ajax({
                url: 'upload.php',
                data: formData,
                type: 'POST',
                contentType: false, 
                processData: false, 
                // ... Other options like success and etc
                beforeSend: function(data, xhr){
                    $('.loader').removeClass("hide");
                },
                success: function(response, status, xhr){
                    var type;
                    if(xhr.status === 200 || xhr.status === '200'){
                        type = "success";
                        $('#Pic').val('');
                    }else{
                        type = "error";
                    }
                    generateAlert(response, type);
                },
                error: function(response, status, xhr){
                    generateAlert("Some Error Occured while uploading file", "error");
                },
                complete: function(response, status, xhr){
                    $('.loader').addClass("hide");
                },
            });
        });
       
    });
我已成功上传图像,图像已显示在文件夹中,但我想将该图像也插入到 mysql 表中

最佳答案

我会用它来捕获文件名并将其存储在数据库中,因为它比存储实际图像更有效率。

$image = ($_FILES['testimage']['name']);

然后您所要做的就是将它作为变量检索,然后引用您在服务器上存储它的位置。例如

<img src="images/<?php echo $Image->testimage; ?>

如果您想将实际图像存储在数据库中,您需要将其存储为 Blob 类型,但由于图像文件的大小,这种方式不是最佳做法。

关于javascript - 图片从上传文件夹插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51032605/

相关文章:

javascript - 将 html append 到 Bootstrap 模式不起作用

javascript - alert() 中的粗体标题效果?

php - 代码点火器 SQL 注入(inject)

php - 在 IIS 上通过 PHP 设置 git 部署时遇到问题

php - 在 OpenShift 上部署 Laravel 应用程序

javascript - 从另一个数组创建一个新数组

javascript - 单击时如何更改按钮的功能?

php pdo更新语句

php - Doctrine2 性能 : Insert/Update multiple rows

MySQL 匿名存储过程