drsvw88664 2017-04-30 16:11
浏览 7

在codeigniter中上传文件

It's my first time using codeigniter. I am totally confused by uploading file in CI. I can insert the data, but the file won't be saved in database mysql

Here's my view - form

    <form action="<?php echo base_url()."crud/do_insert";?>" method="POST" enctype="multipart/form-data">
            <table>
                <tr> 
                    <td> Code </td>
                    <td><input type="text" name="code"></td>
                </tr>
                <tr> 
                    <td> Name </td>
                    <td><input type="text" name="name"></td>
                </tr>
                <tr> 
                    <td> price </td>
                    <td><input type="text" name="price"></td>
                </tr>
                <tr> 
                    <td> Image </td>
                    <td><input type="file" name="image"></td>
                </tr>
            </table>
            <input type="submit" name="insert" value="insert">

And here's my controller

public function do_insert(){
        $code = $_POST['code'];
        $name = $_POST['name'];
        $price= $_POST['price'];
        $image= $_FILES['image'];

        $config['upload_path']          = './upload/';
        $config['allowed_types']        = 'jpg|png';
        $config['max_size']             = 300;
        $config['max_width']            = 2000;
        $config['max_height']           = 2000;

        $this->load->library('upload', $config);

        $image = $this->upload->data();
        $data_insert = array('code' => $code,
                            'name' => $name,
                            'price' => $price,
                            'image' => $image['file_name']
                        );
        $res = $this->m_barang->insertData('stuff', $data_insert);
        if($res>=1){
            redirect('crud/index');
        }else{
            echo "Failed"; 
        }
    }

Is it because I have $_POST and $_FILES that the file wont be inserted in database? Do I have to seperate between text form and upload form but still in one page?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
    • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
    • ¥15 cmd cl 0x000007b
    • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
    • ¥500 火焰左右视图、视差(基于双目相机)
    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号