how can i upload a video and it saved to a folder

2019-04-10 01:21发布

问题:

i'm new to codeigniter. i need help to upload a picture and video and save it to folder and database.

here is my controller

public function upload()
{   
    $this->m_upload->upload();  
    $this->upload_gambar(); 
}

public function upload_gambar()
{
    //load the helper
    $this->load->helper('form');
    $config['upload_path'] = 'assets/gallery/images';

    $config['allowed_types'] = 'gif|jpg|png|mp4';
    $config['max_size'] = '';

    $this->load->library('upload', $config);    
    $this->upload->initialize($config);
    $this->upload->set_allowed_types('*');
    $data['upload_data'] = '';
    $this->upload->do_upload('uploadan');

    redirect(c_upload);
}

and this is my models

public function upload()
{
    $title = $this->input->post('title');
    $details = $this->input->post('details');
    $type = $this->input->post('gallery');
    $picture = $_FILES['uploadan']['name'];

    $data = array(
        'url' => $picture,
        'title' => $title,
        'details' => $details,
        'category' => $type
    );

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

i've already set upload_max_filesize and post_max_size in the php.ini but it still not working. please help me fix this problem. thankyou

回答1:

Try this

In Controller

$configVideo['upload_path'] = 'assets/gallery/images'; # check path is correct
$configVideo['max_size'] = '102400';
$configVideo['allowed_types'] = 'mp4'; # add video extenstion on here
$configVideo['overwrite'] = FALSE;
$configVideo['remove_spaces'] = TRUE;
$video_name = random_string('numeric', 5);
$configVideo['file_name'] = $video_name;

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

if (!$this->upload->do_upload('uploadan')) # form input field attribute
{
    # Upload Failed
    $this->session->set_flashdata('error', $this->upload->display_errors());
    redirect('controllerName/method');
}
else
{
    # Upload Successfull
    $url = 'assets/gallery/images'.$video_name;
    $set1 =  $this->Model_name->uploadData($url);
    $this->session->set_flashdata('success', 'Video Has been Uploaded');
    redirect('controllerName/method');
}

In Model

public function uploadData($url)
{
    $title = $this->input->post('title');
    $details = $this->input->post('details');
    $type = $this->input->post('gallery');

    $data = array(
        'url'       => $url,
        'title'     => $title,
        'details'   => $details,
        'category'  => $type
    );

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


回答2:

Add mimes code for media file in:

application/config/mimes.php

especially for mp4