diff --git a/application/controllers/upload.php b/application/controllers/upload.php index d13a3a5..5669313 100644 --- a/application/controllers/upload.php +++ b/application/controllers/upload.php @@ -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); diff --git a/application/models/fileupload.php b/application/models/fileupload.php index 94c50a5..2927b99 100644 --- a/application/models/fileupload.php +++ b/application/models/fileupload.php @@ -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; + } + } } ?> \ No newline at end of file