简体   繁体   English

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

[英]How to upload files in two different folders using CodeIgniter3?

How to upload images and videos in two different folders using CodeIgniter 3? 如何使用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>

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM