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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?