正在上传数据库和文件目录中的图像codeigniter



这是我的控制器main.php,它正在上传目录中的文件,我想将相同的文件名存储在数据库中,并按名称检索图像。你可以告诉是解决方案代码或编辑我的代码真的需要代码

<?php
       class Main extends CI_controller{ // mian controller
            function index(){
           $this->load->view('main_view.php', array('error'=>''));
                            }
            function upload(){ // upload function for image

                         $config['upload_path'] = './images/'; //directory
                         $config['allowed_types'] = 'jpg|jpeg|gif|png';//type allow
                         $this->load->library('upload',$config);
                            if(! $this->upload->do_upload()){
                            $error = array('error'=>$this->upload->display_errors());
                            $this->load->view('main_view',$error);
                                                              }
                                  else
                                        {
        // 
                                     $file_data = $this->upload->data();
                                     $data['img'] = base_url().'/images/'. $file_data['file_name'] ;
                                     $this->load->view('success_msg',$data);

                                         }
                                                    }
                             }
 ?>

这是我的视图main_view.php

 <html>
    <body>
       <? echo $error;?>
       <? echo form_open_multipart('main/upload'); ?>
           <input type="file" name="userfile" />
           <input type="submit" name="submit" value="Upload" />
             <?php echo form_close();?>
    </body>
    </html>

我想上传数据库中的文件名,可以很容易地检索图像

您可以检索图像名称wth:$this->upload->file_name

编辑:

你的代码中有一些错误。

1) 这个$this->load->view('main_view.php', array('error'=>''));应该是没有.php扩展名的$this->load->view('main_view', array('error'=>''));

2) CI_controller应为CI_Controller

您可以使用这些简单的代码行上传文件夹中的文件,获取文件名并将其存储在数据库中。

$image_path = '../../test_images/'.$_FILES['question_image']['name'];
if(is_uploaded_file($_FILES['question_image']['tmp_name']))
{
  move_uploaded_file($_FILES['question_image']['tmp_name'], $image_path) ;  
}           
$image_url = "test_images/".$_FILES['question_image']['name'];  

$image_path是您想要上传文件的url,$image_url将包含您要存储到数据库中的url

现在您可以通过
检索图像

<img src="<?base_url($image_url)?>" />

最新更新