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条)

报告相同问题?

悬赏问题

  • ¥15 stm32f407使用DMA问题
  • ¥15 您好 这个API接口该怎么弄 网站搭建好了 API也有 现在就不知道该怎么填写API 不知道怎么用
  • ¥88 用uniapp写一个多端的程序,用到高德地图,用高德的JSAPI吗?
  • ¥20 关于#c++#的问题:水果店管理系统
  • ¥30 dbLinq最新版linq sqlite
  • ¥20 对D盘进行分盘之前没有将visual studio2022卸载掉,现在该如何下载回来
  • ¥15 完成虚拟机环境配置,还有安装kettle
  • ¥15 2024年全国大学生数据分析大赛A题:直播带货与电商产品的大数据分析 问题5. 请设计一份优惠券的投放策略,需要考虑优惠券的数量、优惠券的金额、投放时间段和投放商品种类等因素。求具体的python代码
  • ¥15 有人会搭建生鲜配送自营+平台的管理系统吗
  • ¥15 用matlab写代码