【问题标题】:Codeigniter: Upload/Save Image to DatabaseCodeigniter:上传/保存图像到数据库
【发布时间】:2015-04-15 08:49:01
【问题描述】:

我正在尝试在 Codeigniter 项目上创建一个能够上传个人资料图片的用户个人资料编辑表单。当用户单击提交按钮时,我希望允许上传通过。我的表单没有问题(没有错误),但在我设置的上传文件夹中看不到任何图像。任何有助于上传工作的帮助将不胜感激。

控制器(用户编辑/插入值到数据库)

public function update_edit($id) 
 {   

     $config['upload_path'] = '/uploads/profilepics';
     $config['allowed_types'] = 'gif|jpg|png|jpeg';
     $config['max_size'] = 250;
     $this->load->library('upload', $config);
     $this->upload->do_upload('profile_img_path');
     $data_upload_files = $this->upload->data();

     $image = $data_upload_files[full_path]; 
 //input data
 $data = array(
   'user_name' => $this->input->post('user_name'), 
   'email' => $this->input->post('email'),
   'first_name' => $this->input->post('first_name'), 
   'last_name' => $this->input->post('last_name'),
   'city' => $this->input->post('city'), 
   'country' => $this->input->post('country'),
   'description' => $this->input->post('description'),
   'date_edited' => $this->input->post('today'),
   'profile_img_path' => $image 
 ); 
 $this->user_model->update_account($id,$data); 

 //send a confirmation of user account update
 redirect('/');     

}

控制器访问表单

public function update_account()  

 { 

  $id = $this->uri->segment(3); 
  $data['user_profile'] = $this->user_model->get_user_profile($id);
  //only profile owner can access
  if (!$this->uri->segment(3))
   { 
     redirect('404'); 
   } 


  if ($this->is_logged_in()) 
  {  
    if ($this->is_logged_user()) 
    { 

   //update views
   $this->load->view('components/top'); 
   $this->load->view('interact/form/user/edit',$data);  
   $this->load->view('components/bottom'); 
    } 
   else 
    { 
     redirect('403'); 
    } 
  } 
  else 
  { 
    redirect('403');  
  } 
 } 

观点

   <div id="page">  
     <div class="container"> 

       <div class="row"> 

    <h3>Edit Your Account</h3>


   <?php foreach ($user_profile as $user): ?>


<form id="edit_form" class="form-horizontal" method="post" action="<?php echo base_url() . "users/update_edit/" . $user->user_id; ?>" enctype="multipart/form-data">

    <label>Enter a new username: *</label>
    <p>yloko.com/users/user_profile/<?php echo $user->user_name; ?>  </p>
    <br/> 
    <input type="text" name="user_name" value="<?php echo $user->user_name; ?>">     

     <label >Enter a new email*</label>
    <input type="email" name="email" value="<?php echo $user->email; ?>" >

    <label >First Name</label>
    <input type="text" name="first_name" value="<?php echo $user->first_name; ?>" >

     <label >Last Name</label>
    <input type="text" name="last_name" value="<?php echo $user->last_name; ?>" >

     <label >City</label>
    <input type="text" name="city" value="<?php echo $user->city; ?>" >

     <label >Country</label>
    <input type="text" name="country" value="<?php echo $user->country; ?>" >


    <label >Description</label>     
    <textarea name="description"><?php echo $user->description; ?></textarea>

     <!--INPUT-->
     <?php echo $user->profile_img_path; ?>
     <input name = "profile_img_path" type="file" class="input-xlarge" id = "profile_img_path" />

    <!--submit button-->    
    <input type="hidden" name="today" value="<?php echo date('Y-m-d H:i:s'); ?>" >   
    <input type="submit" value="Update">     

</form> 
 <?php endforeach; ?> 
  <a href="<?=site_url('/')?>">Back</a>
  <br/> 

    </div> 
    </div>
  </div>

【问题讨论】:

  • $config['upload_path'] = './uploads/profilepics';验证这一点,您应该使用根目录创建上传文件夹,并且表单标签必须具有属性 enctype="multipart/form-data"

标签: php image file codeigniter upload


【解决方案1】:

你能在库加载后试试这段代码吗?

$this->upload->initialize($config);
if (!$this->upload->do_upload('profile_img_path')) {
    $error = array('error' => $this->upload->display_errors());
    echo $error['error'];
}

让我们知道您遇到的确切错误。

【讨论】:

  • 这就是我得到的:消息:使用未定义的常量 full_path - 假定为 'full_path' 文件名:controllers/users.php 行号:290。看起来我需要放置单引号对于完整路径。
  • 这帮助我指出了错误。刚刚进行了修复,现在可以正常工作。感谢您的帮助。
【解决方案2】:

您应该在表单中包含 enctype

enctype="multipart/form-data"
<form id="edit_form" class="form-horizontal" method="post" action="<?php echo base_url() . "users/update_edit/" . $user->user_id; ?>" enctype="multipart/form-data"> 

【讨论】:

  • 忘记放那部分了。但是,当我尝试它时,它仍然不起作用。
  • @JeffreyTeruel 你能相应地更新你的帖子吗?
【解决方案3】:

$config['upload_path'] = '/uploads/profilepics'; 指向文件系统中的一个文件夹,您可能没有写入权限。使用像$config['upload_path'] = './uploads/profilepics'; 这样的相对路径(./ 是当前目录)。还要确保文件夹存在并且您对其具有写入权限。

【讨论】:

    猜你喜欢
    • 2016-09-05
    • 2021-09-16
    • 1970-01-01
    • 2017-12-20
    • 1970-01-01
    • 2014-05-11
    • 1970-01-01
    • 1970-01-01
    • 2013-06-28
    相关资源
    最近更新 更多