commit 3d9b9dc304493628c3df1498093af514d5f5a5d2
Author: Brückner Tamás <tamas.bruckner@integralvision.hu>
Date:   Tue Apr 5 14:57:27 2016 +0200

    Better file existance check.

diff --git a/upload_qc.module b/upload_qc.module
index 334842d..bd02d72 100644
--- a/upload_qc.module
+++ b/upload_qc.module
@@ -82,11 +82,75 @@ function upload_qc_form($form, &$form_state) {
 }
 
 /**
-* Implements hook_file_validate().
-* This implementation includes two test: filesize and existance of db entry 
-* with the same URI.
-* If it returns errors, the upload process is blocked and the errors are displayed.
-* */
+ * Get unique destination based on db data and file system data.
+ */
+function upload_qc_file_create_filename_db_and_files($destination) {
+  $basename = drupal_basename($destination);
+  $directory = drupal_dirname($destination);
+
+  $basename = preg_replace('/[\x00-\x1F]/u', '_', $basename);
+  if (substr(PHP_OS, 0, 3) == 'WIN') {
+    // These characters are not allowed in Windows filenames
+    $basename = str_replace(array(':', '*', '?', '"', '<', '>', '|'), '_', $basename);
+  }
+
+  // A URI or path may already have a trailing slash or look like "public://".
+  if (substr($directory, -1) == '/') {
+    $separator = '';
+  }
+  else {
+    $separator = '/';
+  }
+
+  $destination = $directory . $separator . $basename;
+
+  if (file_exists($destination) || upload_qc_file_exists_db($destination)) {
+    $pos = strrpos($basename, '.');
+    if ($pos !== FALSE) {
+      $name = substr($basename, 0, $pos);
+      $ext = substr($basename, $pos);
+    }
+    else {
+      $name = $basename;
+      $ext = '';
+    }
+
+    $counter = 0;
+
+    do {
+      $destination = $directory . $separator . $name . '_' . $counter++ . $ext;
+    } while (file_exists($destination) || upload_qc_file_exists_db($destination));
+  }
+
+  return $destination;
+}
+
+/**
+ * Check if file already exists in db.
+ */
+function upload_qc_file_exists_db($destination) {
+  $query_result = db_query(
+    'SELECT fm.fid FROM {file_managed} fm WHERE fm.uri = :uri',
+    array(
+      ':uri' => $destination,
+    )
+  );
+
+  $record = $query_result->fetchObject();
+
+  if (is_object($record)) {
+    return TRUE;
+  }
+
+  return FALSE;
+}
+
+/**
+ * Implements hook_file_validate().
+ * This implementation includes two test: filesize and existance of db entry
+ * with the same URI.
+ * If it returns errors, the upload process is blocked and the errors are displayed.
+ */
 function upload_qc_file_validate($file) {
 // array to return
   $errors = array();
@@ -105,10 +169,9 @@ function upload_qc_file_validate($file) {
 // check if file URI already exists in db.
 // That prevents the AJAX error with duplicate entries for 'uri' in file_managed
   if (!empty($error_check)) {
-    $query_result = db_query('SELECT n.fid
-    FROM {file_managed} n WHERE n.uri = :uri', array(':uri' => $file->destination));
-    $record = $query_result->fetchObject();
-    if (is_object($record)) {
+    // Create destination based on db data.
+    $file->destination = upload_qc_file_create_filename_db_and_files($file->destination);
+    if ($file->destination == FALSE) {
       $errors[] = t("The file's name currently in use. Please wait a few seconds and try again.");
     }
   }
