md5 hash image file and store hash in database to prevent duplicate image uploads.
This commit is contained in:
parent
b0b201467f
commit
5e68235616
2 changed files with 39 additions and 16 deletions
|
@ -23,9 +23,16 @@ class Upload extends CI_Controller {
|
|||
$message = array('type' => 'error', 'status' => $this->upload->display_errors());
|
||||
} else {
|
||||
$upload = $this->upload->data();
|
||||
$hash = md5_file($upload['full_path']);
|
||||
|
||||
$this->load->model('fileupload');
|
||||
$file_name = $this->fileupload->add_upload($upload['file_ext'], $upload['client_name']);
|
||||
$duplicate = $this->fileupload->check_duplicate($hash);
|
||||
|
||||
if($duplicate) {
|
||||
$message = array('type'=>'error', 'status'=>'file already exists:' . $duplicate);
|
||||
} else {
|
||||
$file_name = $this->fileupload->add_upload($upload['file_ext'], $upload['client_name'],$hash);
|
||||
|
||||
rename($upload['full_path'], $upload['file_path'] . $file_name . $upload['file_ext']);
|
||||
|
||||
$config = array(
|
||||
|
@ -41,8 +48,10 @@ class Upload extends CI_Controller {
|
|||
|
||||
$this->load->library('image_lib',$config);
|
||||
$this->image_lib->resize();
|
||||
|
||||
$message = array('type'=>'success','status'=>'Uploaded successfully', 'file'=>base_url($file_name));
|
||||
}
|
||||
}
|
||||
|
||||
echo json_encode($message);
|
||||
}
|
||||
|
|
|
@ -5,10 +5,11 @@ class Fileupload extends CI_Model {
|
|||
parent::__construct();
|
||||
}
|
||||
|
||||
function add_upload($extension, $original_name) {
|
||||
function add_upload($extension, $original_name, $hash) {
|
||||
$data = array(
|
||||
'extension' => $extension,
|
||||
'original_name' => $original_name,
|
||||
'hash' => $hash,
|
||||
);
|
||||
|
||||
$this->db->insert('uploads', $data);
|
||||
|
@ -43,6 +44,19 @@ class Fileupload extends CI_Model {
|
|||
|
||||
return $query->result();
|
||||
}
|
||||
|
||||
function check_duplicate($hash) {
|
||||
$this->db->select('id')->from('uploads')->where('hash',$hash);
|
||||
$query = $this->db->get();
|
||||
|
||||
if($query->num_rows == 0) {
|
||||
return false;
|
||||
} else {
|
||||
$result = $query->result();
|
||||
|
||||
return $result[0]->id;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
Loading…
Reference in a new issue