76 lines
1.9 KiB
PHP
76 lines
1.9 KiB
PHP
<?php
|
|
/**
|
|
* Created by Pensare Software.
|
|
* User: Theo Weijmer
|
|
* Date: 10/01/2018
|
|
* Time: 19:03
|
|
*/
|
|
|
|
class FileUpload extends MY_Controller {
|
|
|
|
private $storagename;
|
|
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
|
|
if ( AUTHENTICATED == FALSE ) die( 'NO ACCESS' );
|
|
|
|
$models = [];
|
|
$libraries = [];
|
|
|
|
$this->load->model( $models );
|
|
$this->load->library( $libraries );
|
|
}
|
|
|
|
public function upload( $foreignID, $tablename )
|
|
{
|
|
$config['upload_path'] = MYROOTPATH . 'documents/temp';
|
|
$config['allowed_types'] = '*';
|
|
$config['remove_spaces'] = FALSE;
|
|
|
|
$this->load->library( 'upload', $config );
|
|
|
|
if ( ! $this->upload->do_upload( 'userfile' ) )
|
|
{
|
|
$error = ['error' => $this->upload->display_errors()];
|
|
|
|
$this->load->view( 'upload_form', $error );
|
|
}
|
|
else
|
|
{
|
|
$data = ['upload_data' => $this->upload->data()];
|
|
|
|
$this->setStoragename();
|
|
|
|
$this->persistDocument( $data['upload_data'], $foreignID, $tablename );
|
|
|
|
$this->renameDocument( $data['upload_data']['full_path'] );
|
|
}
|
|
}
|
|
|
|
private function setStoragename()
|
|
{
|
|
$this->load->helper( 'string' );
|
|
$this->storagename = sha1( random_string( 'alnum', 16 ) );
|
|
}
|
|
|
|
private function persistDocument( $data, $foreignID, $tablename )
|
|
{
|
|
$file = [
|
|
'storagename' => $this->storagename,
|
|
'originalname' => $data['file_name'],
|
|
'user_id' => USERID,
|
|
'uploaddate' => date( 'Y-m-d H:i:s' ),
|
|
'foreign_id' => $foreignID,
|
|
'foreign_table' => $tablename
|
|
];
|
|
|
|
Document::create( $file );
|
|
}
|
|
|
|
private function renameDocument( $original )
|
|
{
|
|
rename( $original, MYROOTPATH . 'documents/' . $this->storagename );
|
|
}
|
|
} |