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';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!