diff --git a/src/Local/LocalFilesystemAdapter.php b/src/Local/LocalFilesystemAdapter.php index 48a2da812..83310f037 100644 --- a/src/Local/LocalFilesystemAdapter.php +++ b/src/Local/LocalFilesystemAdapter.php @@ -4,6 +4,7 @@ namespace League\Flysystem\Local; +use function file_put_contents; use const DIRECTORY_SEPARATOR; use const LOCK_EX; use DirectoryIterator; @@ -97,36 +98,28 @@ public function __construct( public function write(string $path, string $contents, Config $config): void { - $prefixedLocation = $this->prefixer->prefixPath($path); - $this->ensureDirectoryExists( - dirname($prefixedLocation), - $this->resolveDirectoryVisibility($config->get(Config::OPTION_DIRECTORY_VISIBILITY)) - ); - error_clear_last(); - - if (@file_put_contents($prefixedLocation, $contents, $this->writeFlags) === false) { - throw UnableToWriteFile::atLocation($path, error_get_last()['message'] ?? ''); - } - - if ($visibility = $config->get(Config::OPTION_VISIBILITY)) { - $this->setVisibility($path, (string) $visibility); - } + $this->writeToFile($path, $contents, $config); } public function writeStream(string $path, $contents, Config $config): void + { + $this->writeToFile($path, $contents, $config); + } + + /** + * @param resource|string $contents + */ + private function writeToFile(string $path, $contents, Config $config): void { $prefixedLocation = $this->prefixer->prefixPath($path); $this->ensureDirectoryExists( dirname($prefixedLocation), $this->resolveDirectoryVisibility($config->get(Config::OPTION_DIRECTORY_VISIBILITY)) ); - error_clear_last(); - $stream = @fopen($prefixedLocation, 'w+b'); - if ( ! ($stream && false !== stream_copy_to_stream($contents, $stream) && fclose($stream))) { - $reason = error_get_last()['message'] ?? ''; - throw UnableToWriteFile::atLocation($prefixedLocation, $reason); + if (@file_put_contents($prefixedLocation, $contents, $this->writeFlags) === false) { + throw UnableToWriteFile::atLocation($path, error_get_last()['message'] ?? ''); } if ($visibility = $config->get(Config::OPTION_VISIBILITY)) {