如何使用phalcon上传多个图像并将其名称存储在数据库中?

时间:2017-10-04 11:02:28

标签: php mysql phalcon

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

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.");
        }*/
    }
}

上传后,所有图片都成功移至“上传”状态。但是,在数据库中它只存储一个图像名称。那么如何将所有图像名称存储在数据库中?请帮助我,谢谢你的帮助。

这是我的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.");
        }
    });

});

1 个答案:

答案 0 :(得分:0)

尝试添加以下内容:

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

请参阅PHQL documentation