【问题标题】:How to upload multiple images and store their name in database with phalcon?如何使用 phalcon 上传多个图像并将其名称存储在数据库中?
【发布时间】:2018-03-15 18:01:21
【问题描述】:

我创建了一个表单,用户可以上传多个图像,并将上传的图像移动到“上传”文件夹并将他们的名称存储在数据库中。这是我的代码

class ImageController extends Controller
{

    public function imageAction()
    {
       // $mgr_com = new Commons();

        if ($this->request->hasFiles() == true) {
            $uploads = $this->request->getUploadedFiles();
            //print_r($uploads);
            $isUploaded = false;
            foreach ($uploads as $upload) {
                $mgr_com = new Commons();
                $path = strtolower($upload->getname());
                //$path = 'temp / ' . md5(uniqid(rand(), true)) . ' - ' . strtolower($upload->getname());
                //print_r($path);
                $sql= "call img('$path')";
                $results = $mgr_com->getReadConnection()->query($sql);
                ($upload->moveTo($path)) ? $isUploaded = true : $isUploaded = false;

            }

            ($isUploaded) ? die("Files successfully uploaded.") : die("Some error ocurred.");
        } /*else {

            die("You must choose at least one file to send. Please try again.");
        }*/
    }
}

成功上传所有图片后移至“Uploads”文件夹,但在数据库中仅存储一个图片名称。那么如何将所有图像名称存储在数据库中?请帮助我,谢谢你的帮助。

这是我的 jquery

$(document).ready(function () {
    $("#imageupload").change(function () {
        if (typeof (FileReader) != "undefined") {
            var dvPreview = $("#preview-image");
            dvPreview.html("");
            var regex = /^([a-zA-Z0-9\s_\\.\-:])+(.jpg|.jpeg|.gif|.png|.bmp)$/;
            $($(this)[0].files).each(function () {
                var file = $(this);
                if (regex.test(file[0].name.toLowerCase())) {
                    var file_data = $('#imageupload').prop('files')[0];
                    var form_data = new FormData();
                    form_data.append('file', file_data);
                    for (var i = 0; i < file_data.length; i++) {
                        form_data.append("UploadedImage" + i, file_data[i]);
                    }
                    $.ajax({
                        url:'/image/image',
                        method: "POST",
                        data: form_data,
                        processData: false,
                        contentType: false,
                        multiple: true,
                        success: function(response){
                            var result= $.parseJSON(response);
                            if(result.success){
                                $("#image1").html("<img  src=http://localhost/template/fileupload/"+result.userObj.file+">");

                            }else{
                                $("#error").show().html(result.message);
                            }
                        }

                    });
                    var reader = new FileReader();
                    reader.onload = function (e) {
                        var img = $("<img />");
                        img.attr("style", "height:100px;width: 100px");
                        img.attr("src", e.target.result);
                        dvPreview.append(img);
                    }
                    reader.readAsDataURL(file[0]);
                } else {
                    alert(file[0].name + " is not a valid image file.");
                    dvPreview.html("");
                    return false;
                }
            });
        } else {
            alert("This browser does not support HTML5 FileReader.");
        }
    });

});

【问题讨论】:

  • 可以分享FILES的样本数据吗
  • 样本数据是什么意思,我正在上传图片文件
  • 格式file[0]['name']file[1]['name']之类的
  • 我已经更新了我的问题
  • 我不认识 Phalcon。但通常您可以将 user_images 作为另一个表,您可以根据需要在其中拥有 id、image_name、userid 和其他相应的列。因此您可以处理多个上传

标签: php mysql phalcon


【解决方案1】:

尝试添加以下内容:

$results = new Resultset(null,
                         $mgr_com,
                         $mgr_com->getReadConnection()->query($sql));

PHQL documentation

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-15
    • 1970-01-01
    • 2016-10-12
    • 2015-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多