Index: src/wp-includes/media.php
===================================================================
--- src/wp-includes/media.php	(revision 46984)
+++ src/wp-includes/media.php	(working copy)
@@ -4294,10 +4294,18 @@
 		$path = substr( $path, strlen( $dir['baseurl'] . '/' ) );
 	}
 
-	$sql = $wpdb->prepare(
-		"SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_wp_attached_file' AND meta_value = %s",
-		$path
-	);
+	if ( $wpdb->has_cap( 'utf8mb4' ) ) {
+		// Use case-sensitive query, if possible.
+		$sql = $wpdb->prepare(
+			"SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_wp_attached_file' AND meta_value = CONVERT( %s USING utf8mb4 ) COLLATE utf8mb4_bin",
+			$path
+		);
+	} else {
+		$sql = $wpdb->prepare(
+			"SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_wp_attached_file' AND meta_value = %s",
+			$path
+		);
+	}
 
 	$post_id = $wpdb->get_var( $sql );
 
Index: tests/phpunit/tests/media.php
===================================================================
--- tests/phpunit/tests/media.php	(revision 46984)
+++ tests/phpunit/tests/media.php	(working copy)
@@ -1058,7 +1058,10 @@
 		$this->assertEquals( $attachment_id, attachment_url_to_postid( $image_url ) );
 	}
 
-	function test_attachment_url_to_postid_schemes() {
+	/**
+	 * @ticket 33109
+	 */
+	function test_attachment_url_to_postid_with_different_scheme() {
 		$image_path    = '2014/11/' . $this->img_name;
 		$attachment_id = self::factory()->attachment->create_object(
 			$image_path,
@@ -1069,13 +1072,35 @@
 			)
 		);
 
-		/**
-		 * @ticket 33109 Testing protocols not matching
-		 */
 		$image_url = 'https://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $image_path;
 		$this->assertEquals( $attachment_id, attachment_url_to_postid( $image_url ) );
 	}
 
+	/**
+	 * @ticket 39768
+	 */
+	function test_attachment_url_to_postid_should_be_case_sensitive() {
+		global $wpdb;
+
+		if ( ! $wpdb->has_cap( 'utf8mb4' ) ) {
+			$this->markTestSkipped( 'This test requires utf8mb4 support in MySQL.' );
+		}
+
+		$image_path    = '2014/11/' . $this->img_name;
+		$attachment_id = self::factory()->attachment->create_object(
+			$image_path,
+			0,
+			array(
+				'post_mime_type' => 'image/jpeg',
+				'post_type'      => 'attachment',
+			)
+		);
+
+		$image_path = '2014/11/' . ucfirst( $this->img_name );
+		$image_url  = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $image_path;
+		$this->assertNotEquals( $attachment_id, attachment_url_to_postid( $image_url ) );
+	}
+
 	function test_attachment_url_to_postid_filtered() {
 		$image_path    = '2014/11/' . $this->img_name;
 		$attachment_id = self::factory()->attachment->create_object(
