图片文件空无一人

时间:2017-03-13 10:04:21

标签: php jquery ajax codeigniter

我已经创建了一个脚本来上传带有文本的图像,但问题是当我们发布数据并上传图像时文件变空了我不知道为什么文件变空了任何人都可以帮助我获取正确的文件

这是我的ajax脚本

$(document).ready(function(e) {
$("#post").on('submit', (function(e) {
    $("#load").show();

    var form = this;
    var formData = new FormData(this);
    alert(formData);
    formData.append('post_dt', $("#contentbox").html());

    $.ajax({
        url         : "http://domainname.com/demo/forums/post_forum",
        type        : "POST",
        data        : formData,
        contentType : false,
        cache       : false,
        processData : false,
        success     : function(data) {
            $("#data_update").prepend($(data).fadeIn('slow'));
            $("#contentbox").empty();
            form.reset();
        },
        complete: function() {
            $("#load").hide();
        }
    });
}));
});

这是正在创建的html表单

<form method="POST" action="" id="post" enctype="multipart/form-data" onsubmit="return false;">
                <ul>
                    <li>
                        <i class="fa fa-photo"></i> Upload A Photo / Document
                        <input type="file" name="image" />
                    </li>
                </ul>
                <div id='display'></div>
                <div id="contentbox" contenteditable="true" name="post_dt">
                </div>

                <input type="submit" id="sb_art" class="btn_v2" value="Start Discussion" />
            </form>

我在控制器中创建的PHP脚本

public function post_forum() {
    $config['upload_path']   = './assets/uploads/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size']      = 3000;
    $this->load->library('upload', $config);

    if(!empty($this->input->post('image'))) {
        if (!$this->upload->do_upload('image')) {
            $error = array('error' => $this->upload->display_errors());
        } else {
            $dt = $this->upload->data();
            $file = $dt['file_name'];
        }
    } else {
        $file = 'Filename.jpg';
    }
    $word_dta    = $this->input->post('post_dt');
    $time_posted = time();
    $user_id     = $this->basic_model->is_login($this);

    $data_upload = array(
        'upload_document' => $file,
        'discussion'      => $word_dta,
        'user_id'         => $user_id,
        'time_posted'     => $time_posted,
        'status'          => 'Posted'
    );

    $post_id = $this->basic_model->insertRecord($data_upload, 'forums');

    $data = array(
        'file_name'   => $file,
        'data'        => $word_dta,
        'time_posted' => $time_posted,
        'post_id'     => $post_id,
        'name'        => $this->basic_model->getUserData(),
        'command'     => 'Submit Post'
    );

    $this->load->view('forum/includes/get_data', $data);
}

3 个答案:

答案 0 :(得分:0)

id作为imageFieldId添加到html文件中。然后尝试

        var file_data = $('#imageFieldId').prop('files')[0];   
        var form_data = new FormData();                  
        form_data.append('file', file_data);
       //formData.append('post_dt', $("#contentbox").html());

    $.ajax({.....

答案 1 :(得分:0)

我已经解决了我的问题我在控制器中犯了一个错误,这是我的错误所以我已经解决了我的问题需要注意

答案 2 :(得分:-1)

确保您的网络服务器(nginx / apache)上传大小限制正确无误。

此链接可能是您设置上传大小的参考:

Increase File Upload Size Limit

Edit NginX Conf to Increase File Size Upload