douming4359 2017-09-06 08:19
浏览 64
已采纳

如何使用CodeIgniter 3在两个不同的文件夹中上传文件?

How to upload images and videos in two different folders using CodeIgniter 3?

I have done only images. Please guide me through videos. I want to upload image and videos in two different folders.

My Controller, Model and View

  class Blog extends CI_Contrller{
       public function create_blog(){
            //  Check Login
            if(!$this->session->userdata('logged_in')){
                redirect('blogs/login');
            }
            $data['title'] = 'Create Blog';

            $this->form_validation->set_rules('title', 'Title', 'required');
            $this->form_validation->set_rules('content', 'Content', 'required');

            if($this->form_validation->run() === FALSE){
                $this->load->view('templates/header');
                $this->load->view('blog/create_blog', $data);
                $this->load->view('templates/footer');
            } else {
                // Upload Image
                $path = './assets/uploads/blogs/images';
                $config['upload_path'] = $path;
                $config['allowed_types'] = 'gif|jpg|png';
                $config['max_size'] = 15000;
                $config['max_width'] = 1024;
                $config['max_height'] = 768;

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

                if(!$this->upload->do_upload()){
                    $this->session->set_flashdata('file_error', $this->upload->display_errors());
                    $post_image = 'noimage.jpg';
                    redirect('blog/create_blog');
                }else{
                    $data = array('upload_data' => $this->upload->data());
                    $post_image = $_FILES['userfile']['name'];
                }

                $this->M_blog->create_blog($post_image);
                redirect('blog/view');
            }
        }
     }

Model

     // Model Begins here                                                                  
      public function create_blog($post_image){
            $slug = url_title($this->input->post('title'));

            $data = array(
                    'title' => $this->input->post('title'),
                    'slug' => $slug,
                    'content' => $this->input->post('content'),
                    'image' => $post_image
                );

            return $this->db->insert('events', $data);
        }

View

    // View begins here                                                                   
    <div class="container"><br>
      <h2><?= $title ?></h2>


        <?php echo form_open_multipart('blog/create_blog'); ?>
        <?php echo validation_errors(); ?>
        <?php echo $this->session->flashdata('file_error'); ?>

          <div class="form-group">
          <label>Title</label>
          <input type="text" class="form-control" name="title" 
           placeholder="Add Title">
        </div>
       <div class="form-group">
        <label>Body</label>
        <textarea class="form-control" id="editor1" name="content" 
          placeholder="Add Body"></textarea>
      </div>
     <div class="form-group">
        <label>Upload Image</label>
        <input type="file" name="userfile" id="userfile" size="20" />
     </div>
     <button type="submit" class="btn btn-default">Submit</button>
    </form>
   </div>
  • 写回答

1条回答 默认 最新

  • dqxyh48864 2017-09-06 08:47
    关注

    Try this

    class Blog extends CI_Contrller{
    function create_blog() {
        //  Check Login
        if (!$this->session->userdata('logged_in')) {
          redirect('blogs/login');
        }
        $data['title'] = 'Create Blog';
    
        $this->form_validation->set_rules('title', 'Title', 'required');
        $this->form_validation->set_rules('content', 'Content', 'required');
    
        if ($this->form_validation->run() === FALSE) {
          $this->load->view('templates/header');
          $this->load->view('blog/create_blog', $data);
          $this->load->view('templates/footer');
        } else {
          // Upload Image
    
    
          $ext = pathinfo($_FILES['userfile']['name'], PATHINFO_EXTENSION);
          $ext = strtolower($ext);
          $image_ext_arr = array('gif', 'jpg', 'png', 'jpeg');
    
          if (in_array($ext, $image_ext_arr)) {
            $path = FCPATH.'assets/uploads/blogs/images';
            $allowed_types = 'gif|jpg|png';
          } else {
            $path = FCPATH.'assets/uploads/blogs/video';
            $allowed_types = 'mp4|mp3';
          }
    
          $config['upload_path'] = $path;
          $config['allowed_types'] = $allowed_types;
          $config['max_size'] = 15000;
          $config['max_width'] = 1024;
          $config['max_height'] = 768;
    
          $this->load->library('upload', $config);
    
          if (!$this->upload->do_upload()) {
            $this->session->set_flashdata('file_error', $this->upload->display_errors());
            $post_image = 'noimage.jpg';
            redirect('blog/create_blog');
          } else {
            $data = array('upload_data' => $this->upload->data());
            $post_image = $_FILES['userfile']['name'];
          }
    
          $this->M_blog->create_blog($post_image);
          redirect('blog/view');
        }
      }
    }
    

    Added few line :

    $ext = pathinfo($_FILES['userfile']['name'], PATHINFO_EXTENSION); //to find whether is image or video
          $ext = strtolower($ext);// some time extension in upper later so converted into small letters
          $image_ext_arr = array('gif', 'jpg', 'png', 'jpeg'); //give here all extension of image that you want to upload
    
          if (in_array($ext, $image_ext_arr)) { //if matches means is image
            $path = FCPATH.'assets/uploads/blogs/images';
            $allowed_types = 'gif|jpg|png';
          } else { //else video
            $path = FCPATH.'assets/uploads/blogs/video';
            $allowed_types = 'mp4|mp3';
          }
    

    Note : Better to give absolute path instead of relative path to upload so

    $path = FCPATH.'assets/uploads/blogs/video';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀