dsfdsf8888 2018-12-02 12:54
浏览 110
已采纳

Codeigniter文件从没有ajax / jquery / javascript的表单上传

Context : I am updating user_profile data after user registration using form #updateprofileform , i am able to process all other form fields using standard validations and insert/update using user_model. issue is with resume field which should accept a file as input type and upload to server and store its path on server (say http://domain/uploads/$filename) to a column in database.

Expectation A logged in user , able to view his details(if already in database),update details if incomplete profile.

Question : I found methods how we can upload a file to server using CI framework or how we can upload a file in this context using jquery/ajax/java-script. But i am looking for simple solution for above problem which can do the job with out dependency on technologies.

user_profile.php (view)

<table>
<form class="row" name="updateprofile" action = "<?php echo base_url()?>user/user_profile" method="POST">
<tr>
        <td><label for="email">Email ID</label></td><td><input class="form-control" name="email" placeholder="Email-ID" type="text" value="<?php if (isset($email)) echo $email;?>" /> <span style="color:red"><?php  echo form_error('email'); ?></span></td>
    </tr>
  <tr>
        <td><label for="resume">Resume</label></td>
        <td>
          <input name="resume" placeholder="Upload resume" type="file" value="<?php if (isset($resume)) echo $resume;?>" />
          <span style="color:red"><?php  echo form_error('resume'); ?>
          </span>
        </td>
    </tr>
  <tr>
        <td></td>
        <td><div class="col-md-6 col-lg-3"> <button type="submit" class="btn btn--primary type--uppercase" name="updateprofile" value="updateprofile" formaction = "<?php echo base_url()?>user/user_profile">Update Profile</button> </div></td>
    </tr>
  </form>
</table>

user_controller

class User extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
$config = array(
        'upload_path' => "./uploads/",
        'allowed_types' => "doc|docx|pdf",
        'overwrite' => TRUE,
        'max_size' => "2048000", // Can be set to particular file size , here it is 2 MB(2048 Kb)
        //'max_height' => "768",
        //'max_width' => "1024"
        );
        $this->load->library('upload', $config);
    }
  public function user_profile()
    {  
      $resume = $this->input->post('resume');
  if ($this->user_model->set_user_profile($id,FALSE) )
            { if($this->do_upload($resume)){
                $this->session->set_flashdata('msg_success','Updation Successful!');
                  echo validation_errors();}
            }
            else
            {
                $this->session->set_flashdata('msg_error','Error! Please try again later.');
                redirect('user/user_profile');
            }
  public function do_upload($resume){

    if($this->upload->do_upload($resume))
    {
    $data = array('upload_data' => $this->upload->data($resume));

    $this->load->view('user_profile',$data);
    }
    else
    {
    $error = array('error' => $this->upload->display_errors($resume));
    $this->load->view('user_profile', $error);
    }
    }

currently always error.... Error! Please try again later . so appreciate some pointers here to upload both file and form data together and save file location in database without ajax/jquery.

Thanks

  • 写回答

2条回答 默认 最新

  • drxrgundk062317205 2018-12-03 09:37
    关注

    Resolved ! 4 important steps needed and some missing in most Q&A in all other posts to make this complete.will share samplecode in github when i get sometime.

    1. form encryption type must be enctype="multipart/form-data" (thanks to @Javier Larroulet and @dexter )
    2. do_upload method by default look for file field with name=userfile (Thanks to @Alex ! for referencing this), so, if you have your form's file field name different than userfile, then only it is required to mention as do_upload('filefieldname') otherwise optional
    3. loading of upload library configuration is tricky.. if you autoupload the upload library , then load->library upload config will fail!, you just =>initialize it.$this->upload->initialize($config); if you are not using autoload for upload library, then yes, you can load it from controller. $this->load->library('upload', $config);
    4. when ever, you are using upload, you have to catch the form validation errors and data errors separately (again thanks to @Alex for pointing this)

    a nice documentation is given from codeigniter at this link (https://www.codeigniter.com/userguide3/libraries/file_uploading.html#the-controller ) but doesn't highlight routine mistakes.

    hope this post help someone not to waste another 24 hours to debug.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?