<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * Download class
 */

class Download extends MY_Controller
{

    /**
     * 모델을 로딩합니다
     */
    protected $models = array();

    /**
     * 헬퍼를 로딩합니다
     */
    protected $helpers = array('form', 'array');

    function __construct()
    {
        parent::__construct();

        /**
         * 라이브러리를 로딩합니다
         */
        $this->load->library(array('querystring'));
    }

    /**
     * 첨부파일 다운로드 하기
     */
    public function downfiles($file_id = 0)
    {

        $file_id = (int) $file_id;
        if (empty($file_id) OR $file_id < 1) {
            show_404();
        }

        $this->load->model(array('Upload_file_model'));

        $file = $this->Upload_file_model->get_one($file_id);

        if ( ! element('ufi_id', $file)) {
            show_404();
        }

        $user_id = (int) $this->userlib->item('user_id');

        if ( ! $this->session->userdata('upload_file_download_' . element('ufi_id', $file))) {

            $this->session->set_userdata('upload_file_download_' . element('ufi_id', $file), '1');
            $this->Upload_file_model->update_plus(element('ufi_id', $file), 'ufi_download', 1);

        }


        $this->load->helper('download');

        // Read the file's contents
        $data = file_get_contents(config_item('uploads_dir') . '/files/' . element('ufi_filename', $file));
        $name = element('ufi_originname', $file);
        force_download($name, $data);

    }
}
