【问题标题】:Uploading image in database and file directory codeigniter [closed]在数据库和文件目录codeigniter中上传图像[关闭]
【发布时间】:2012-09-04 02:32:34
【问题描述】:

这是我的控制器 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>

我想在数据库中上传文件名,并且可以轻松检索图像

【问题讨论】:

    标签: file codeigniter upload


    【解决方案1】:

    您可以通过$this-&gt;upload-&gt;file_name检索图像名称

    编辑:

    您的代码中有一些错误。

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

    2) CI_controller 应该是 CI_Controller

    【讨论】:

      【解决方案2】:

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

      $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)?>" />
      

      【讨论】:

      • 他已经上传了正确的CodeIgniter类的图片。
      猜你喜欢
      • 2012-10-20
      • 1970-01-01
      • 1970-01-01
      • 2013-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-05
      相关资源
      最近更新 更多