【问题标题】:Image Upload in CodeIgniter using PHP使用 PHP 在 CodeIgniter 中上传图片
【发布时间】:2017-04-28 11:36:26
【问题描述】:

我有一个视图,用户可以在其中编辑他的详细信息。

查看

<form class="form-horizontal" method ="post" action="<?php echo site_url('studentDashboardController/saveUserDetails');?>">
<?php echo form_open('studentDashboardController/saveUserDetails'); ?>
<?php echo $this->session->flashdata('msg'); ?>
<fieldset>

    <!-- Form Name -->
    <legend>User Details</legend>

    <!-- Text input-->
    <div class="form-group">
        <label class="col-md-4 control-label" for="name">Full Name</label>
        <div class="col-md-8">
            <input id="name" name="name" type="text" class="form-control input-md" value="<?php foreach($details as $detail){?><?php echo $detail->name?><?php }?>">
        </div>
    </div>

    <!-- Text input-->
    <div class="form-group">
        <label class="col-md-4 control-label" for="dob">Date of Birth</label>
        <div class="col-md-8">
            <input id="dob" name="dob" type="text" placeholder="" class="form-control input-md" value="">
        </div>
    </div>

    ...

    <!-- File Button -->
    <div class="form-group">
        <label class="col-md-4 control-label" for="userfile">Upload Profile Picture</label>
        <div class="col-md-4">
            <input id="userfile" name="userfile" class="input-file" type="file">
        </div>
    </div>

    <!-- Button -->
    <div class="form-group">
        <label class="col-md-4 control-label" for="submit"></label>
        <div class="col-md-4">
            <button id="submit" name="submit" type="submit" class="btn btn-primary">Save Changes</button>
        </div>
    </div>

控制器

public function  saveUserDetails()
{
    if (isset($this->session->userdata['logged_in']))
    {
        $uid = ($this->session->userdata['logged_in']['userId']);

        $data['notifyCount']=  $this->notifications->getUnreadNotification($uid);
        $data['title']=  $this->notifications->getUnreadNotificationTitle($uid);
        $data['notifyCount']=  $this->notifications->getUnreadNotification($uid);
        $data['users'] = $this->user->getUserNames();

        $config['upload_path'] = './assets/img/taro/profilePictures/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '10000';
        $config['max_width']  = '5000';
        $config['max_height']  = '3000';
        $this->load->library('upload', $config);

        $this->upload->do_upload();
        $upload_data = $this->upload->data();

        $msg = $this->studentprofileModel->saveUserDetails($uid,$this->input->post());
        $msgText = $msg['msg'];
        $msgType = $msg['type'];
        //Loading View
        if($msgType == "danger")
        {
            $this->session->set_flashdata('msg', '<div class="alert alert-danger text-center">'.$msgText.'</div>');
        }
        else
        {
            $this->session->set_flashdata('msg', '<div class="alert alert-success text-center">'.$msgText.'</div>');
        }

        redirect(base_url('index.php/studentDashboardController/loadGeneralProfilePage'));
    }
    else
    {
        $this->load->view('login/loginView');
    }
}

我需要做的是首先通过调用studentprofileModel's saveUserDetails方法保存通过表单收集的用户详细信息。在这种方法中,我需要发送上传图像的名称,以便我可以将图像路径存储在 db 表的图像字段中。我还需要将图像上传到资产文件夹的subdirectory taro

我已完成以下操作,表单字段中的数据在数据库中得到更新。但是图像文件没有上传。

在这方面的任何建议都将受到高度赞赏

【问题讨论】:

  • 您遇到任何错误?通过此功能检查上传错误$this-&gt;upload-&gt;display_errors()); 并在您的表单标签中添加enctype="multipart/form-data"
  • 您的视图有&lt;form class...,同时也在调用&lt;?php echo form_open(...。这不是在&lt;form&gt; 中给你一个&lt;form&gt; 吗?

标签: php codeigniter file-upload upload image-uploading


【解决方案1】:

在进一步测试之前,我的第一个猜测是将enctype="multipart/form-data" 属性添加到您的&lt;form&gt; 标记中,因为它指定表单数据在提交到服务器时应如何编码,multipart/form-data 是当表单包含任何 &lt;input type="file"&gt; 时需要。

您可以阅读有关此here 的更多信息。


更新 2:

实际上,您似乎打开了两个表单标签?一个来自&lt;form&gt;,另一个来自form_open() CI提供的功能? 如果是这样,请仅使用其中之一。

如果你选择在 HTML 中做:

&lt;form class="form-horizontal" method ="post" action="&lt;?php echo site_url('studentDashboardController/saveUserDetails');?&gt;" enctype="multipart/form-data"&gt;

如果你选择使用 CI 的功能,有两种方法:form_openform_open_multipart(这和form_open 完全一样,但它会自动添加多部分的东西)

form_open('studentDashboardController/saveUserDetails', array('enctype' =&gt; "multipart/form-data"))

form_open_multipart('studentDashboardController/saveUserDetails')


更新 3:

  • 确保要上传图片的文件夹有足够的权限;

  • 在这里传递绝对路径$config['upload_path'] = './assets/img/taro/profilePictures/'而不是相对路径

  • 另外,将此$this-&gt;upload-&gt;do_upload(); 更改为$this-&gt;upload-&gt;do_upload('userfile');

让我们知道结果:)

【讨论】:

  • 嗨,我已将上述内容添加到 HTML 代码中。但是,我认为我的控制器有问题。你介意检查一下吗?会很有帮助。
  • 嗨@NayantaraJeyaraj。刚刚更新了答案。你能检查一下吗?谢谢
【解决方案2】:

您可以在您的控制器“function saveUserDetails()”中添加此代码

   public function  saveUserDetails()
  {
      if (isset($this->session->userdata['logged_in']))
      {
         $uid = ($this->session->userdata['logged_in']['userId']);
         $post_data = $this->input->post();
       if($_FILES['image']['name'])
      {
          //load upload library
          $this->load->library('upload');

          // Specify configuration for File 
          $config['upload_path'] = './assets/img/taro/profilePictures/';
          $config['allowed_types'] = 'gif|jpg|png';
          $config['max_size'] = '10000';
          $config['max_width']  = '5000';
          $config['max_height']  = '3000';
          $this->upload->initialize($config);

          if ($this->upload->do_upload('image')){
               $image_data = $this->upload->data();
              $post_data['image'] = $image_data['file_name'];
          }
          else{
              $error = $this->upload->display_errors();
         }
       }

        $msg = $this->studentprofileModel->saveUserDetails($uid,$post_data);
        $msgText = $msg['msg'];
        $msgType = $msg['type'];
        .....
   }

别忘了给图片文件夹权限。

希望对你有用!!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-25
    • 2018-02-17
    • 1970-01-01
    • 2015-10-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多