Store uploads in database and name them after their id.
This commit is contained in:
parent
7872cabd90
commit
7f88eb83df
2 changed files with 31 additions and 2 deletions
|
@ -8,7 +8,9 @@ class Upload extends CI_Controller {
|
|||
public function process() {
|
||||
header('Content-Type: application/json',true);
|
||||
|
||||
$config['file_name'] = 135;
|
||||
$temp_name = md5(rand());
|
||||
|
||||
$config['file_name'] = $temp_name;
|
||||
$config['max_size'] = 2048;
|
||||
$config['upload_path'] = './uploads/';
|
||||
$config['allowed_types'] = 'gif|jpg|jpeg|png|bmp';
|
||||
|
@ -17,7 +19,14 @@ class Upload extends CI_Controller {
|
|||
if (!$this->upload->do_upload('image')) {
|
||||
$message = array('type' => 'error', 'status' => $this->upload->display_errors());
|
||||
} else {
|
||||
$message = array('type'=>'success','status'=>'Uploaded successfully','file'=>'http://screens.p5dev.com/' . $config['file_name']);
|
||||
$upload = $this->upload->data();
|
||||
|
||||
$this->load->model('fileupload');
|
||||
$file_name = $this->fileupload->add_upload($upload['file_ext'], $upload['orig_name']);
|
||||
|
||||
rename($upload['full_path'], $upload['file_path'] . $file_name . $upload['file_ext']);
|
||||
|
||||
$message = array('type'=>'success','status'=>'Uploaded successfully', 'file'=>'http://screens.p5dev.com/' . $file_name);
|
||||
}
|
||||
|
||||
echo json_encode($message);
|
||||
|
|
20
application/models/fileupload.php
Normal file
20
application/models/fileupload.php
Normal file
|
@ -0,0 +1,20 @@
|
|||
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
|
||||
|
||||
class Fileupload extends CI_Model {
|
||||
function __construct() {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
function add_upload($extension, $original_name) {
|
||||
$data = array(
|
||||
'extension' => $extension,
|
||||
'original_name' => $original_name,
|
||||
);
|
||||
|
||||
$this->db->insert('uploads', $data);
|
||||
|
||||
return $this->db->insert_id();
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
Loading…
Reference in a new issue