首页 > 解决方案 > 从没有 ajax/jquery/javascript 的表单上传 Codeigniter 文件

问题描述

上下文:我在使用表单 #updateprofileform 用户注册后更新 user_profile 数据,我能够使用标准验证处理所有其他表单字段并使用 user_model 插入/更新。问题在于resume应该接受文件作为输入类型并上传到服务器并将其在服务器上的路径(例如http://domain/uploads/ $filename)存储到数据库中的列的字段。

期望一个登录的用户,能够查看他的详细信息(如果已经在数据库中),如果不完整的个人资料更新详细信息。

问题:我找到了如何使用 CI 框架将文件上传到服务器的方法,或者我们如何使用 jquery/ajax/java-script 在此上下文中上传文件。但我正在为上述问题寻找简单的解决方案,它可以在不依赖技术的情况下完成这项工作。

user_profile.php(查看)

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

用户控制器

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);
    }
    }

目前总是错误....错误!请稍后再试 。所以在这里感谢一些指针,将文件和表单数据一起上传,并在没有 ajax/jquery 的情况下将文件位置保存在数据库中。

谢谢

标签: phpcodeignitercodeigniter-3form-submitzend-form-element

解决方案


解决 !需要 4 个重要步骤,并且在所有其他帖子的大多数问答中都缺少一些步骤来完成此操作。当我有时间时,将在 github 中分享示例代码。

  1. 表单加密类型必须是 enctype="multipart/form-data" (感谢 @Javier Larroulet 和 @dexter )
  2. do_upload 方法默认查找 name=userfile 的文件字段(感谢@Alex!引用此文件),因此,如果您的表单文件字段名称与userfile不同,则只需提及 do_upload('filefieldname' ) 否则可选
  3. 加载上传库配置很棘手.. 如果您自动上传上传库,那么 load->library upload config 将失败!,你只需 =>初始化它。$this->upload->initialize($config); 如果您没有使用自动加载来上传库,那么是的,您可以从控制器加载它。$this->load->library('upload', $config);
  4. 每当您使用上传时,您必须分别捕获表单验证错误和数据错误(再次感谢@Alex 指出这一点)

codeigniter 在此链接 ( https://www.codeigniter.com/userguide3/libraries/file_uploading.html#the-controller ) 上提供了一个很好的文档,但没有突出显示常规错误。

希望这篇文章可以帮助某人不要再浪费 24 小时进行调试。


推荐阅读