我如何上传视频并将其保存到codeigniter中的文件夹中?

时间:2016-01-19 10:30:25

标签: php codeigniter

我是codeigniter的新手。我需要帮助上传图片和视频,并将其保存到文件夹和数据库。

这是我的控制器

<?php
session_start();

// If user already logged in, send them to index
if ( ! empty( $_SESSION['user'] ) ) {
    header('Location:index.php?');
    exit;
}

if(isset($_POST["button_login"]))           //FUNGSI LOGIN
{ 

    $user   = $_POST["name"];
    $pass   = $_POST["password"];

    $sql_login      = "SELECT * FROM user WHERE username='$user' AND password='$pass'";
    $result_login   = $conn->query($sql_login);
    $row_login      = $result_login->fetch_assoc();
    $numrow_login   = $result_login->num_rows;

    if($numrow_login==1)
    {
        $_SESSION['user'] = $user;
        header('Location:index.php?');
        exit;
    }
}
?>

这是我的模特

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

我已经在php.ini中设置了upload_max_filesize和post_max_size,但它仍然无效。请帮我解决这个问题。三江源

2 个答案:

答案 0 :(得分:3)

试试这个

在控制器中

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

在模型中

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

答案 1 :(得分:3)

在以下位置添加媒体文件的mimes代码:

application/config/mimes.php

特别是对于mp4