php - 从没有 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 的情况下将文件位置保存在数据库中。
谢谢
解决方案
解决 !需要 4 个重要步骤,并且在所有其他帖子的大多数问答中都缺少一些步骤来完成此操作。当我有时间时,将在 github 中分享示例代码。
- 表单加密类型必须是 enctype="multipart/form-data" (感谢 @Javier Larroulet 和 @dexter )
- do_upload 方法默认查找 name=userfile 的文件字段(感谢@Alex!引用此文件),因此,如果您的表单文件字段名称与userfile不同,则只需提及 do_upload('filefieldname' ) 否则可选
- 加载上传库配置很棘手.. 如果您自动上传上传库,那么 load->library upload config 将失败!,你只需 =>初始化它。$this->upload->initialize($config); 如果您没有使用自动加载来上传库,那么是的,您可以从控制器加载它。$this->load->library('upload', $config);
- 每当您使用上传时,您必须分别捕获表单验证错误和数据错误(再次感谢@Alex 指出这一点)
codeigniter 在此链接 ( https://www.codeigniter.com/userguide3/libraries/file_uploading.html#the-controller ) 上提供了一个很好的文档,但没有突出显示常规错误。
希望这篇文章可以帮助某人不要再浪费 24 小时进行调试。
推荐阅读
- python - 在 Python 中从不同的元组创建对
- java - 为什么 textview.setText() 在某个函数中工作?
- push-notification - 有没有办法追踪未确认推送通知的更多细节
- javascript - 如何获取使用onClick打开的图像选择器来选择一个?
- c - 从套接字读取时附加到缓冲区
- c++ - '结构因对齐说明符而被填充'VS警告
- javascript - 如何在 React js 中显示图像?
- python - 通过 Linux 从 django 管理面板调用 python 脚本
- macos - 如何在 arm64 主机平台上运行 amd64 docker 镜像
- amazon-web-services - QlikSense 的 AWS FSx 文件系统的替代方案