Php 如何在CodeIgniter根文件夹和数据库中保存图像?

Php 如何在CodeIgniter根文件夹和数据库中保存图像?,php,html,mysql,codeigniter,Php,Html,Mysql,Codeigniter,我需要让用户能够更改配置文件图像,但我无法通过教学演练找到显示CodeIgniter上载过程如何工作的清晰文档 我已经在主表中创建了一个名为user\u profile的列 I控制器I定义大小,类型,并对其进行加密 public function update_profile(){ $config["upload_path"] = "public/images/profile"; $config["allowed_types"] = "jpg|jpeg|gif|png";

我需要让用户能够更改配置文件图像,但我无法通过教学演练找到显示CodeIgniter上载过程如何工作的清晰文档

我已经在主表中创建了一个名为
user\u profile
的列

I控制器I定义大小类型,并对其进行加密

public function update_profile(){

    $config["upload_path"] = "public/images/profile";
    $config["allowed_types"] = "jpg|jpeg|gif|png";
    $config["max_size"] = 700;
    $config["max_width"] = 1024;
    $config["max_height"] = 768;
    $config["encrypt_name"] = TRUE;

    $this->load->library("upload", $config);

    if($this->upload->do_upload('user_profile'))
    {
        $info_arquivo = $this->upload->data();
        $user_profile = $info_arquivo["file_name"];

        $this->load->model("profile_model");

        $image_perfil = array(
            "user_profile" => $user_profile
        );

        $query = $this->profile_model->insert_image($image_perfil);

        if($query){
            $this->session->set_flashdata('msg', 'Imagem de perfilfoi atualizada');
            redirect(base_url('admin/dashboard'), 'refresh');
        } else {
            $this->session->set_flashdata('msg', 'Imagem de perfil não foi atualizada');
            redirect(base_url('admin/profile'), 'refresh');
        }
    }
    else
    {
        $this->session->set_flashdata('msg', 'Imagem de perfil não foi atualizada');
        redirect(base_url('admin/profile'), 'refresh');
    }

}
admin/profile
视图中,我放置了以下表单

                <!-- UPLOAD IMAGE USER -->
                <?php echo form_open_multipart('admin/update_profile'); ?>
                    <div    class="form-group">
                        <label>Selecione uma imagem</label>
                        <input type="file" name="user_profile" class="form-control" id="user_profile" required/>
                    </div>
                    <div    class="form-group">
                        <button type="submit" class="btn btn-success pull-right" value="cadastrar">Cadastrar</button>
                    </div>
                </form>
                <!-- END UPLOAD IMAGE USER -->

我认为你在表单URL上的错误。请在下面的HTML表单中添加index.php

   <!-- UPLOAD IMAGE USER -->
            **<?php echo form_open_multipart('index.php/admin/update_profile'); ?>**
                <div    class="form-group">

****
公共功能更新\u配置文件()
{
$config['upload_path']=“public/images/profile/”;
$config['allowed_types']='gif | jpg | png';
$config['max_size']=1000;
$config['max_width']=1024;
$config['max_height']=768;
$this->load->library('upload',$config);
如果(!$this->upload->do\u upload('user\u profile')){
$error=array('error'=>$this->upload->display_errors());
回声“;
 <!DOCTYPE html>
 <html>
 <head>
<title></title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">

<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

<!-- Popper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>

<!-- Latest compiled JavaScript -->
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
  </head>
  <body>

   <form action="<?php echo base_url('stackoverflow/update_profile');?>" method="post" enctype="multipart/form-data">
    <div class="form-group">
        <label>Selecione uma imagem</label>
        <input type="file" name="user_profile" class="form-control" id="user_profile" required/>
    </div>
    <div    class="form-group">
        <button type="submit" class="btn btn-success pull-right" value="cadastrar">Cadastrar</button>
    </div>
</form>

</body>
</html>
打印错误($error); 回声“;
 <!DOCTYPE html>
 <html>
 <head>
<title></title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">

<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

<!-- Popper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>

<!-- Latest compiled JavaScript -->
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
  </head>
  <body>

   <form action="<?php echo base_url('stackoverflow/update_profile');?>" method="post" enctype="multipart/form-data">
    <div class="form-group">
        <label>Selecione uma imagem</label>
        <input type="file" name="user_profile" class="form-control" id="user_profile" required/>
    </div>
    <div    class="form-group">
        <button type="submit" class="btn btn-success pull-right" value="cadastrar">Cadastrar</button>
    </div>
</form>

</body>
</html>
模具(); $this->session->set_flashdata('msg','Imagem de perfil não foi atualizada'); 重定向(基本url('admin/profile'),'refresh'); //$this->load->view('upload\u form',$error); } 否则{ $info_arquivo=$this->upload->data(); $user_profile=$info_arquivo[“文件名”]; $this->load->model(“profile_model”); $image\u perfil=数组( “用户配置文件”=>$user\u profile ); $query=$this->profile\u model->insert\u image($image\u perfil); 如果($query){ $this->session->set_flashdata('msg','Imagem de perfilfio atualizada'); 重定向(基本url(“管理/仪表板”),“刷新”); }否则{ $this->session->set_flashdata('msg','Imagem de perfil não foi atualizada'); 重定向(基本url('admin/profile'),'refresh'); } } }
看法



请你缩小你的问题范围,把注意力集中在问题所在?问题是我仍然无法保存文件,我相信我的逻辑是错误的。试着按照以下步骤进行:然后根据你的情况进行调整。解释得很好……“我仍然无法保存文件”:有错误吗?无法上载文件?或者无法将数据保存到数据库?
 <!DOCTYPE html>
 <html>
 <head>
<title></title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">

<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

<!-- Popper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>

<!-- Latest compiled JavaScript -->
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
  </head>
  <body>

   <form action="<?php echo base_url('stackoverflow/update_profile');?>" method="post" enctype="multipart/form-data">
    <div class="form-group">
        <label>Selecione uma imagem</label>
        <input type="file" name="user_profile" class="form-control" id="user_profile" required/>
    </div>
    <div    class="form-group">
        <button type="submit" class="btn btn-success pull-right" value="cadastrar">Cadastrar</button>
    </div>
</form>

</body>
</html>