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

时间:2017-09-06 08:19:29

标签: php codeigniter video

如何使用CodeIgniter 3在两个不同的文件夹中上传图像和视频?

我只做过图片。请引导我完成视频。我想在两个不同的文件夹中上传图片和视频。

我的控制器,型号和视图

  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 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 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 个答案:

答案 0 :(得分:1)

试试这个

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');
    }
  }
}

添加了几行:

$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';
      }

注意:最好提供绝对路径而不是上传的相对路径

$path = FCPATH.'assets/uploads/blogs/video';