1
0
Fork 0

md5 hash image file and store hash in database to prevent duplicate image uploads.

This commit is contained in:
Andrew Tomaka 2011-11-08 15:40:41 -05:00
parent b0b201467f
commit 5e68235616
2 changed files with 39 additions and 16 deletions

View file

@ -23,25 +23,34 @@ 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']);
rename($upload['full_path'], $upload['file_path'] . $file_name . $upload['file_ext']);
$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(
'image_library' => 'gd2',
'source_image' => $upload['file_path'] . $file_name . $upload['file_ext'],
'create_thumb' => true,
'maintain_ratio' => true,
'width' => 175,
'height' => 175,
'new_image' => './thumbs/' . $file_name . $upload['file_ext'],
'thumb_marker' => '',
);
$config = array(
'image_library' => 'gd2',
'source_image' => $upload['file_path'] . $file_name . $upload['file_ext'],
'create_thumb' => true,
'maintain_ratio' => true,
'width' => 175,
'height' => 175,
'new_image' => './thumbs/' . $file_name . $upload['file_ext'],
'thumb_marker' => '',
);
$this->load->library('image_lib',$config);
$this->image_lib->resize();
$message = array('type'=>'success','status'=>'Uploaded successfully', 'file'=>base_url($file_name));
$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);

View file

@ -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;
}
}
}
?>