--- /Users/oli/Desktop/download_count/download_count.module
+++ download_count.module
@@ -163,22 +163,24 @@
     }
   }
   $filepath = file_create_path($filename);
-  $result = db_query("SELECT u.nid, f.filepath FROM {upload} u JOIN {files} f ON f.fid = u.fid WHERE f.filepath = '%s'", $filepath);
+	$result = db_query("SELECT u.nid, f.filepath FROM {files} f LEFT JOIN {upload} u ON f.fid = u.fid WHERE f.filepath = '%s'", $filepath);
   if ($file = db_fetch_object($result)) {
-    if (user_access('view uploaded files') && node_access('view', node_load($file->nid))) {
+		if($file->nid) {
+			if (user_access('view uploaded files') && node_access('view', node_load($file->nid))) {
+				watchdog('download', 'Failed to download %file', array('%file' => $filename), WATCHDOG_WARNING);
+			}
+		}
+		else {
       watchdog('download', '%file was downloaded', array('%file' => $filename), WATCHDOG_NOTICE);
       // If the file is already added, just increment the count,
       // otherwise add the file with count 1
       if(db_result(db_query("SELECT filename FROM {file_downloads} WHERE filename = '%s'", $filename))) {
         db_query("UPDATE {file_downloads} SET count = count+1, timestamp = %d  WHERE filename = '%s'", time(), $filename);
       }
-    else {
-        db_query("INSERT INTO {file_downloads} (filename, count, timestamp) VALUES ('%s', 1,%d)", $filename, time());
-    }
-  }
-  else {
-    watchdog('download', 'Failed to download %file', array('%file' => $filename), WATCHDOG_WARNING);
-  }
+	    else {
+				db_query("INSERT INTO {file_downloads} (filename, count, timestamp) VALUES ('%s', 1,%d)", $filename, time());
+	    }
+		}
   }
 }
 /**
