diff --git a/src/Illuminate/Filesystem/Filesystem.php b/src/Illuminate/Filesystem/Filesystem.php index 67a8682b0509..8b804b3dec04 100644 --- a/src/Illuminate/Filesystem/Filesystem.php +++ b/src/Illuminate/Filesystem/Filesystem.php @@ -499,6 +499,20 @@ public function isWritable($path) return is_writable($path); } + /** + * Determine if two files are the same by comparing their hashes. + * + * @param string $firstFile + * @param string $secondFile + * @return bool + */ + public function hasSameHash($firstFile, $secondFile) + { + $hash = @md5_file($firstFile); + + return $hash && $hash === @md5_file($secondFile); + } + /** * Determine if the given path is a file. * diff --git a/src/Illuminate/Support/Facades/File.php b/src/Illuminate/Support/Facades/File.php index c22d363a8555..f35edc90fb97 100755 --- a/src/Illuminate/Support/Facades/File.php +++ b/src/Illuminate/Support/Facades/File.php @@ -15,6 +15,7 @@ * @method static bool deleteDirectory(string $directory, bool $preserve = false) * @method static bool exists(string $path) * @method static bool isDirectory(string $directory) + * @method static bool isEqual(string $file, string $compared) * @method static bool isFile(string $file) * @method static bool isReadable(string $path) * @method static bool isWritable(string $path) diff --git a/tests/Filesystem/FilesystemTest.php b/tests/Filesystem/FilesystemTest.php index a5457daea994..89e66fb9690d 100755 --- a/tests/Filesystem/FilesystemTest.php +++ b/tests/Filesystem/FilesystemTest.php @@ -544,6 +544,21 @@ public function testCopyCopiesFileProperly() $this->assertEquals($data, file_get_contents(self::$tempDir.'/text/foo2.txt')); } + public function testHasSameHashChecksFileHashes() + { + $filesystem = new Filesystem; + + mkdir(self::$tempDir.'/text'); + file_put_contents(self::$tempDir.'/text/foo.txt', 'contents'); + file_put_contents(self::$tempDir.'/text/foo2.txt', 'contents'); + file_put_contents(self::$tempDir.'/text/foo3.txt', 'invalid'); + + $this->assertTrue($filesystem->hasSameHash(self::$tempDir.'/text/foo.txt', self::$tempDir.'/text/foo2.txt')); + $this->assertFalse($filesystem->hasSameHash(self::$tempDir.'/text/foo.txt', self::$tempDir.'/text/foo3.txt')); + $this->assertFalse($filesystem->hasSameHash(self::$tempDir.'/text/foo4.txt', self::$tempDir.'/text/foo.txt')); + $this->assertFalse($filesystem->hasSameHash(self::$tempDir.'/text/foo.txt', self::$tempDir.'/text/foo4.txt')); + } + public function testIsFileChecksFilesProperly() { $filesystem = new Filesystem;