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,25 +23,34 @@ class Upload extends CI_Controller {
|
||||||
$message = array('type' => 'error', 'status' => $this->upload->display_errors());
|
$message = array('type' => 'error', 'status' => $this->upload->display_errors());
|
||||||
} else {
|
} else {
|
||||||
$upload = $this->upload->data();
|
$upload = $this->upload->data();
|
||||||
|
$hash = md5_file($upload['full_path']);
|
||||||
|
|
||||||
$this->load->model('fileupload');
|
$this->load->model('fileupload');
|
||||||
$file_name = $this->fileupload->add_upload($upload['file_ext'], $upload['client_name']);
|
$duplicate = $this->fileupload->check_duplicate($hash);
|
||||||
rename($upload['full_path'], $upload['file_path'] . $file_name . $upload['file_ext']);
|
|
||||||
|
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(
|
$config = array(
|
||||||
'image_library' => 'gd2',
|
'image_library' => 'gd2',
|
||||||
'source_image' => $upload['file_path'] . $file_name . $upload['file_ext'],
|
'source_image' => $upload['file_path'] . $file_name . $upload['file_ext'],
|
||||||
'create_thumb' => true,
|
'create_thumb' => true,
|
||||||
'maintain_ratio' => true,
|
'maintain_ratio' => true,
|
||||||
'width' => 175,
|
'width' => 175,
|
||||||
'height' => 175,
|
'height' => 175,
|
||||||
'new_image' => './thumbs/' . $file_name . $upload['file_ext'],
|
'new_image' => './thumbs/' . $file_name . $upload['file_ext'],
|
||||||
'thumb_marker' => '',
|
'thumb_marker' => '',
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->load->library('image_lib',$config);
|
$this->load->library('image_lib',$config);
|
||||||
$this->image_lib->resize();
|
$this->image_lib->resize();
|
||||||
$message = array('type'=>'success','status'=>'Uploaded successfully', 'file'=>base_url($file_name));
|
|
||||||
|
$message = array('type'=>'success','status'=>'Uploaded successfully', 'file'=>base_url($file_name));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
echo json_encode($message);
|
echo json_encode($message);
|
||||||
|
|
|
@ -5,10 +5,11 @@ class Fileupload extends CI_Model {
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
}
|
}
|
||||||
|
|
||||||
function add_upload($extension, $original_name) {
|
function add_upload($extension, $original_name, $hash) {
|
||||||
$data = array(
|
$data = array(
|
||||||
'extension' => $extension,
|
'extension' => $extension,
|
||||||
'original_name' => $original_name,
|
'original_name' => $original_name,
|
||||||
|
'hash' => $hash,
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->db->insert('uploads', $data);
|
$this->db->insert('uploads', $data);
|
||||||
|
@ -43,6 +44,19 @@ class Fileupload extends CI_Model {
|
||||||
|
|
||||||
return $query->result();
|
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