load->model( $models ); $this->load->library( $libraries ); } public function upload( $foreignID ) { $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->json( $error ); } else { $data = ['upload_data' => $this->upload->data()]; $this->setStoragename(); $this->persistDocument( $data['upload_data'], $foreignID ); $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 ) { $file = [ 'storagename' => $this->storagename, 'originalname' => $data['file_name'], 'user_id' => USERID, 'uploaddate' => date( 'Y-m-d H:i:s' ), 'foreign_id' => $foreignID ]; Document::create( $file ); } private function renameDocument( $original ) { rename( $original, MYROOTPATH . 'documents/' . $this->storagename ); } }