Failed Upload Files Codeigniter

ε祈祈猫儿з 提交于 2021-01-07 03:52:09

问题


I'm try to make crud with upload file but somehow it's keep return FALSE but i don't know what's wwrong with my code if i var_dump($data) before condition upload it's show the name of my image but if i var_dump($uploads) it's keep show false

This is my controller

public function saveReimburse()
{
    validate_submitted_data(array(
        'nama' => 'required',
        'category_reimburse_id' => 'required',
        'amount' => 'required|numeric',
        'date_reimburse' => 'required',
    ));
    // data
    $data = [
        'nama' => $this->input->post('nama'),
        'category_reimburse_id' => $this->input->post('category_reimburse_id'),
        'amount' => $this->input->post('amount'),
        'date_reimburse' => $this->input->post('date_reimburse'),
        'photo' => $_FILES['photo'],
    ];

    // condition
    $date = date('Y-m-d');
    $date = strtotime($date);
    $date = strtotime('-7 day', $date);

    if ($data['date_reimburse'] < date('Y-m-d', $date)) {
        echo json_encode(array('succes' => FALSE, 'message' => 'Max Reimburse was 1 week ago'));
    } else {
        if ($data['photo'] = "") {
        } else {
            $config = [
                'upload_path' => './assets/reimburse',
                'allowed_types' => 'jpg|png|gif',
                'overwrite' => TRUE
            ];
            $this->load->library('upload', $config);
            $upload = $this->upload->do_upload('photo');

            var_dump($upload);exit;
            if (!$upload) {
                json_encode(array('success' => FALSE, 'message' => 'Failed Upload'));
                redirect('Reimburse/index', 'refresh');
            } else {
                $this->upload->data('file_name');
                $save = $this->reimburseModel->saveReimburse('reimburse', $data);
                var_dump($data);exit;
                if (!$save) {
                    echo json_encode(array('success' => FALSE, 'message' => 'Failed to reccord'));
                } else {
                    redirect('Reimburse/index', 'refresh');
                    echo json_encode(array('success' => TRUE, 'message' => 'Reimburse Success'));
                }
            }
        }
    }
}

and this my model

function saveReimburse($table,$data)
{
    $this->load->database('default', TRUE);
    if(!$this->db->insert($table,$data))
        return FALSE;
    $data["id"] = $this->db->insert_id();

    return (object) $data;
} 

This is my input code

<?php echo form_open_multipart(get_uri("Reimburses/saveReimburse"), array("id" => "formReimburse", "class" => "general-form", "role" => "form")); ?>
<div id="expense-dropzone" class="post-dropzone">
    <div class="modal-body clearfix">
        <!-- <form action =" " method='POST'> -->
            <div class="form-group">
                <label for="Nama">Nama</label>
                <input type="text" class="form-control" id="nama" name="nama" placeholder="Nama">
            </div>
            <div class="form-group">
                <label for="category_reimburse_id">Category</label>
                <select class="form-control form-control-lg" name="category_reimburse_id">
                <option value ="">-</option>
                <?php 
                foreach($category as $ct){?>
                    <option value ="<?php echo $ct->id ?>"><?php echo $ct->category ?></option>
                    <?php }?>
                </select>
            </div>
            <div class="form-group">
                <label for="amount">Amount</label>
                <input type="text" class="form-control" id="amount" name="amount" placeholder="Amount">
            </div>
            <div class="form-group">
                <label for="date_reimburse">Date</label>
                <input type="date" class="form-control" id="date_reimburse" name="date_reimburse" value='<?php echo date('Y-m-d') ?>'>
            </div>
            <div class="form-group">
                <div class="form-group">
                    <label for="photo">Input Photo</label>
                    <input type="file" class="form-control-file" id="photo" name ="photo">
                </div>
            </div>
            <div class="float-right">
                <button type="cancel" class="btn btn-warning ">Cancel</button>
                <button type="submit" class="btn btn-primary ">Submit</button>
            </div>
        <!-- </form> -->
    </div>
</div>
<?php echo form_close() ?>

回答1:


Check the error using the error function

print_r($this->upload->display_errors());



回答2:


Try adding bellow upload path

'upload_path' => '../assets/reimburse';



回答3:


Put one / after reimburse like "./assets/reimburse/" can solve problem may be and assets folder in root directory



来源:https://stackoverflow.com/questions/60445579/failed-upload-files-codeigniter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!