doudouji2016 2015-08-20 12:51
浏览 20

在codeigniter中保存两个具有相同名称的图像

I try more and more but I am unable two save two files with different names in the database.

My code is here:

if(!empty($_FILES['image']['name']))
    {

                $config['upload_path'] = 'uploads/content/';
        $config['allowed_types'] = 'gif|jpg|jpeg|png|JPEG|PNG|GIF|bmp';

                        /*      */
              $newFileName = $_FILES['image']['name'];
              $fileExt = @array_pop(explode(".", $newFileName));
              $filename = md5(time().$newFileName).".".$fileExt;
              $config['file_name'] = $filename;
              $_POST['image'] = $filename;
                        /*      */
              $this->load->library('upload', $config);

       if((! $this->upload->do_upload('image')) && $_FILES['image']['error'] != 4)
        {
            $upload_error = $this->upload->display_errors();
            if(!empty($upload_error))
            {
             $this->session->set_flashdata('imageResponse', '<div class="alert alert-danger" role="alert">Image upload is not possible.only allowed gif or jpg or jpeg or png or gif or bmp</div>');
                                       if($type=='install')
                                      {
                                   redirect(base_url().'addcontentinstallation');
                                      }
                                      if($type=='video')
                                      {
                                   redirect(base_url().'addcontentvedio');
                                      }
                                      if($type=='banner')
                                      {
                                   redirect(base_url().'addcontentbanner');
                                      }
                                      if($type=='website')
                                      {
                                   redirect(base_url().'addcontentwebsite');
                                      }

                                 //redirect('addcontent');
            }
        }
        $data = $this->upload->data();
        //$_POST['image'] = $data['orig_name'];

        $this->upload->do_upload(); 
    }
    $_POST['image']=!empty($_POST['image'])?$_POST['image']:"";

        if(!empty($_FILES['title_image']['name']))
    {

                $config['upload_path'] = 'uploads/content/';
        $config['allowed_types'] = 'gif|jpg|jpeg|png|JPEG|PNG|GIF|bmp';

                             /*      */
              $newFileName = $_FILES['title_image']['name'];
              $fileExt = @array_pop(explode(".", $newFileName));
              $filename = md5(time().$newFileName).".".$fileExt;
              $config['title_image'] = $filename;
              $_POST['image'] = $filename;
                        /*      */
              $this->load->library('upload', $config);  

       if((! $this->upload->do_upload('title_image')) && $_FILES['title_image']['error'] != 4)
        {
            $upload_error = $this->upload->display_errors();
            if(!empty($upload_error))
            {
             $this->session->set_flashdata('imageResponse', '<div class="alert alert-danger" role="alert">Image upload is not possible.only allowed gif or jpg or jpeg or png or gif or bmp</div>');
             if($type=='install')
                                    {
                                 redirect(base_url().'addcontentinstallation');
                                    }
                                    if($type=='video')
                                    {
                                 redirect(base_url().'addcontentvedio');
                                    }
                                    if($type=='banner')
                                    {
                                 redirect(base_url().'addcontentbanner');
                                    }
                                    if($type=='website')
                                    {
                                 redirect(base_url().'addcontentwebsite');
                                    }
            }
        }
        $data = $this->upload->data();
        //$_POST['title_image'] = $data['orig_name'];

        $this->upload->do_upload(); 
    }
    $_POST['title_image']=!empty($_POST['title_image'])?$_POST['title_image']:"";
          $this->content_model->add_conetent($_POST);

$this->session->set_flashdata('imageResponse', '<div class="alert alert-success" role="alert">Success! Offer Content added successfully!</div>');
           if($type=='install')
           {
        redirect(base_url().'addcontentinstallation');
           }
           if($type=='video')
           {
        redirect(base_url().'addcontentvedio');
           }
           if($type=='banner')
           {
        redirect(base_url().'addcontentbanner');
           }
           if($type=='website')
           {
        redirect(base_url().'addcontentwebsite');
           }
}

I search more I find for that I need to use this

 $this->upload->initialize($this->set_upload_options($filename));

before the ! $this->upload->do_upload('title_image')) && $_FILES['title_image']['error'] != 4) and

$this->upload->initialize($this->set_upload_options($filename));
before the ! $this->upload->do_upload('image')) && $_FILES['title_image']

['error'] != 4) but I that case I got error that The upload path does not appear to be valid but I am unable to do this ..

 private function set_upload_options($filename_multi)
{   
    //  upload an image options
    $config = array();
    $config['upload_path']   = 'assets/uploads/logo/';
    $config['allowed_types'] = 'gif|jpg|png|jpeg|bmp|GIF|JPG|JPEG|jpeg|BMP';
    $config['max_size']      = '30000';
    $config['overwrite']     = FALSE;
    $config['file_name'] = $filename_multi;
    return $config;
}

Please help me.

  • 写回答

2条回答 默认 最新

  • dongtigai3875 2015-08-20 12:57
    关注

    create unique filename like this

    $file_extension = pathinfo($_FILES["title_image"]["name"], PATHINFO_EXTENSION);
    $file_name = pathinfo($_FILES["title_image"]["name"], PATHINFO_FILENAME);
    $filename = md5(time() . $file_name ) . '.' . $file_extension;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算