dsa1234569
2014-02-12 18:03
浏览 49
已采纳

使用php codeigniter无法在mysql数据库中上传文件[复制]

This question already has an answer here:

I want to upload a file in mysql database using php codeigniter. when I upload a file it don't show any error and file doesn't upload.and the value of column file_name in mysql shows as NULL. Here is my view code :

                    <div class="box">
                    <div class="box-header"><span class="title"><?php echo get_phrase('diagnosis_report');?></span></div>
                    <div class="box-content">
                    <table cellpadding="0" cellspacing="0" border="0" class="table table-normal ">
                        <thead>
                            <tr>
                                <td><div>#</div></th>
                                <td><div><?php echo get_phrase('report_type');?></div></td>
                                <td><div><?php echo get_phrase('document_type');?></div></td>
                                <td><div><?php echo get_phrase('download');?></div></td>
                                <td><div><?php echo get_phrase('description');?></div></td>
                                <td><div><?php echo get_phrase('date');?></div></td>
                                <td><div><?php echo get_phrase('laboratorist');?></div></td>
                                <td><div><?php echo get_phrase('option');?></div></td>
                            </tr>
                        </thead>
                        <tbody>
                            <?php 
                            $count = 1;
                            $diagnostic_reports =   $this->db->get_where('diagnosis_report' , array('prescription_id' => $prescription_id))->result_array();
                            foreach($diagnostic_reports as $row2):?>
                            <tr>
                                <td><?php echo $count++;?></td>
                                <td><?php echo $row2['report_type'];?></td>
                                <td><?php echo $row2['document_type'];?></td>
                                <td style="text-align:center;">
                                    <?php if($row2['document_type'] == 'image'):?>
                                    <div id="thumbs">
                                        <a href="<?php echo base_url();?>uploads/diagnosis_report/<?php echo $row2['file_name'];?>" 
                                            style="background-image:url(<?php echo base_url();?>uploads/diagnosis_report/<?php echo $row2['file_name'];?>)" title="<?php echo $row2['file_name'];?>">
                                                </a></div>
                                    <?php endif;?>

                                    <a href="<?php echo base_url();?>uploads/diagnosis_report/<?php echo $row2['file_name'];?>" target="_blank"
                                        class="btn btn-blue">   <i class="icon-download-alt"></i> <?php echo get_phrase('download');?></a>
                                </td>
                                <td><?php echo $row2['description'];?></td>
                                <td><?php echo date('d M,Y', $row2['timestamp']);?></td>
                                <td><?php echo $this->crud_model->get_type_name_by_id('laboratorist',$row2['laboratorist_id'],'name');?></td>
                                <td align="center">
                                    <a href="<?php echo base_url();?>index.php?laboratorist/manage_prescription/delete_diagnosis_report/<?php echo $row2['diagnosis_report_id'];?>/<?php echo $prescription_id;?>" onclick="return confirm('delete?')"
                                        rel="tooltip" data-placement="top" data-original-title="<?php echo get_phrase('delete');?>" class="btn btn-red">
                                            <i class="icon-trash"></i>
                                                </a>
                                </td>
                            </tr>
                            <?php endforeach;?>
                        </tbody>
                    </table>
                    </div>
                </div>

                <!------DIAGNOSTIC REPORT FOR THIS PRESCRIPTION---->


                <!------ADD A DIAGNOSTIC REPORT TO THIS PRESCRIPTION-->
                <div class="box">
                    <div class="box-header"><span class="title"><?php echo get_phrase('add_diagnosis_report');?></span></div>
                    <div class="box-content">
                    <?php echo form_open('laboratorist/manage_prescription/create_diagnosis_report' , array('class' => 'form-horizontal validatable'));?>
                        <div class="padded">
                            <div class="control-group">
                                <label class="control-label"><?php echo get_phrase('report_type');?></label>
                                <div class="controls">
                                    <input type="text" name="report_type"  /> <span class="label label-blue">report type can be x-ray, blood-test etc.</span>
                                </div>
                            </div>
                            <div class="control-group">
                                <label class="control-label"><?php echo get_phrase('document_type');?></label>
                                <div class="controls">
                                    <select name="document_type" >
                                        <option value="image"><?php echo get_phrase('image');?></option>
                                        <option value="doc" ><?php echo get_phrase('doc');?></option>
                                        <option value="pdf"><?php echo get_phrase('pdf');?></option>
                                        <option value="excel"><?php echo get_phrase('excel');?></option>
                                        <option value="other"><?php echo get_phrase('other');?></option>
                                    </select>
                                </div>
                            </div>
                            <div class="control-group">
                                <label class="control-label"><?php echo get_phrase('upload_document');?></label>
                                <div class="controls">
                                    <input type="file" name="userfile" />
                                </div>
                            </div>
                            <div class="control-group">
                                <label class="control-label"><?php echo get_phrase('description');?></label>
                                <div class="controls">
                                    <textarea name="description" ></textarea>
                                </div>
                            </div>
                            <div class="control-group">

                                <div class="controls">
                                    <input type="hidden" name="prescription_id" value="<?php echo $prescription_id;?>" />
                                    <button type="submit" class="btn btn-blue"><?php echo get_phrase('add_diagnosis_report');?></button>
                                </div>
                            </div>

                        </div>
                    </form>
                    </div>
                </div>

and here is my code to upload file in database :

    function manage_prescription($param1 = '', $param2 = '', $param3 = '')
{
    if ($this->session->userdata('laboratorist_login') != 1)
        redirect(base_url() . 'index.php?login', 'refresh');



    if ($param1 == 'create_diagnosis_report') {
        $data['report_type']     = $this->input->post('report_type');
        $data['document_type']   = $this->input->post('document_type');
        $data['prescription_id'] = $this->input->post('prescription_id');
        $data['description']     = $this->input->post('description');
        $data['timestamp']       = strtotime(date('Y-m-d') . ' ' . date('H:i:s'));
        $data['laboratorist_id'] = $this->session->userdata('laboratorist_id');
        move_uploaded_file($_FILES["userfile"]["tmp_name"] , "uploads/diagnosis_report/".$_FILES["userfile"]["name"]);                  
        $data['file_name'] = $_FILES["userfile"]["name"];

        $this->db->insert('diagnosis_report', $data);
        $this->session->set_flashdata('flash_message', get_phrase('diagnosis_report_created'));
        redirect(base_url() . 'index.php?laboratorist/manage_prescription/edit/' . $this->input->post('prescription_id'), 'refresh');
    }

    if ($param1 == 'delete_diagnosis_report') {
        $this->db->where('diagnosis_report_id', $param2);
        $this->db->delete('diagnosis_report');
        $this->session->set_flashdata('flash_message', get_phrase('diagnosis_report_deleted'));
        redirect(base_url() . 'index.php?laboratorist/manage_prescription/edit/' . $param3, 'refresh');

    } else if ($param1 == 'edit') {
        $page_data['edit_profile'] = $this->db->get_where('prescription', array(
            'prescription_id' => $param2
        ))->result_array();
    }
    $page_data['page_name']     = 'manage_prescription';
    $page_data['page_title']    = get_phrase('manage_prescription');
    $page_data['prescriptions'] = $this->db->get('prescription')->result_array();
    $this->load->view('index', $page_data);
}
</div>
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongyuqie4322 2014-02-12 18:06
    已采纳

    You have to use form_open_multipart() not form_open() in your view file

    打赏 评论

相关推荐 更多相似问题