上传后,Dropzone JS在数据库中设置图像文件路径

时间:2019-03-06 22:07:36

标签: javascript php multipartform-data dropzone.js

我正在使用dropzone制作多张照片上传表单:

HTML:

<form id="eventPhotosForm" method="post" action="" enctype="multipart/form-data">
    <div class="dropzone" id="eventPhotosDropzone"><div class="dz-message" data-dz-message><span><i class="fas fa-image"></i><br>Add Event Photos</span></div></div>
</form>

JS:

$("#eventPhotosDropzone").dropzone({
    url: "process_upload_event_images.php",
    autoProcessQueue: true,
    maxFiles: 4,
    maxFilesize: 2,
    uploadMultiple: false,
    acceptedFiles: ".jpeg,.jpg,.png",
    init: function () {

        var eventPhotosDropzone = this;

        eventPhotosDropzone.on('success', function (file, response) {

                var form = document.getElementById('eventPhotosForm');

                var theFilesInput = document.createElement('input');
                theFilesInput.setAttribute('type', 'hidden');
                theFilesInput.setAttribute('name', 'theFiles[]');
                theFilesInput.setAttribute('value', file.upload.filename);

                form.appendChild(theFilesInput);

                var event_id = $('.page_inner').prop("id");

                var eventIDInput = document.createElement('input');
                eventIDInput.setAttribute('type', 'hidden');
                eventIDInput.setAttribute('name', 'event_id');
                eventIDInput.setAttribute('value', event_id);

                form.appendChild(eventIDInput);

                form.submit();

         });

    }
});

Dropzone正在将照片正确地上传到我的服务器,但是在上传了照片之后,我想在数据库中设置新上传的照片的路径名:

PHP:

$theFiles = $_POST['theFiles'];

if (!empty($theFiles)) {

    $event_id = $_POST['event_id'];

    foreach($theFiles as $theFile)
    {   
        $t = time();
        $fileNames[] = $t . "_" . $theFile;
    }

    $event_image_one = $fileNames[0];
    $event_image_two = $fileNames[1];
    $event_image_three = $fileNames[2];
    $event_image_four = $fileNames[3];

    sw::shared()->events->addPhotos(
        $event_id,
        $event_image_one,
        $event_image_two,
        $event_image_three,
        $event_image_four,
        $error
    );

尽管我为“ theFiles []”添加了隐藏的输入,但在PHP端却没有通过,因此事件照片路径从未更新,因为代码从未执行

如何解决此问题,以便可以上传多张照片并在数据库中设置路径?

谢谢!

0 个答案:

没有答案