{"record":{"id":"8c9ca273dfa297c9","repo":"thephpleague/flysystem","slug":"unable-to-get-checksum-for-path-reason-8c9ca2","errorCode":null,"errorMessage":"Unable to get checksum for $path: $reason","messagePattern":"Unable to get checksum for \\$path: \\$reason","errorType":"exception","errorClass":"UnableToProvideChecksum","httpStatus":null,"severity":"error","filePath":"src/CalculateChecksumFromStream.php","lineNumber":22,"sourceCode":"namespace League\\Flysystem;\n\nuse function hash_final;\nuse function hash_init;\nuse function hash_update_stream;\n\ntrait CalculateChecksumFromStream\n{\n    private function calculateChecksumFromStream(string $path, Config $config): string\n    {\n        try {\n            $stream = $this->readStream($path);\n            $algo = (string) $config->get('checksum_algo', 'md5');\n            $context = hash_init($algo);\n            hash_update_stream($context, $stream);\n\n            return hash_final($context);\n        } catch (FilesystemException $exception) {\n            throw new UnableToProvideChecksum($exception->getMessage(), $path, $exception);\n        }\n    }\n\n    /**\n     * @return resource\n     */\n    abstract public function readStream(string $path);\n}\n","sourceCodeStart":4,"sourceCodeEnd":31,"githubUrl":"https://github.com/thephpleague/flysystem/blob/b277b5dc3d56650b68904117124e79c851e12376/src/CalculateChecksumFromStream.php#L4-L31","documentation":"The CalculateChecksumFromStream trait (used by Filesystem itself, PathPrefixedAdapter, and ReadOnlyFilesystemAdapter) computes checksums by opening a read stream and hashing it. Any FilesystemException raised by readStream() — most commonly UnableToReadFile because the path does not exist or is not readable — is wrapped as UnableToProvideChecksum with message 'Unable to get checksum for $path: $reason'. This is the fallback checksum path used when the adapter does not implement ChecksumProvider or rejected the requested algorithm.","triggerScenarios":"$filesystem->checksum($path) on an adapter without native checksum support (InMemory, Zip, WebDAV via decorators, PathPrefixedAdapter wrapping such adapters) when the file is missing; also when Filesystem::checksum() fell back to stream hashing after ChecksumAlgoIsNotSupported and the file cannot be read.","commonSituations":"Verifying an artifact that was never written or was already consumed/deleted; read-only filesystem wrappers over sparse data; path prefix bugs in PathPrefixedAdapter making every read miss.","solutions":["Call $filesystem->fileExists($path) first and handle missing files explicitly.","Inspect $e->getPrevious() — it is the original FilesystemException (usually UnableToReadFile) whose reason pinpoints the miss.","Fix the path/prefix or write the file before checksumming.","For adapters that can provide checksums natively, make sure the real adapter (not a decorator stripping the interface) handles ChecksumProvider so you get clearer, metadata-based errors."],"exampleFix":"// before\n$fs = new Filesystem(new InMemoryFilesystemAdapter());\n$fs->checksum('file.txt'); // nothing written -> UnableToProvideChecksum\n\n// after\nif ($fs->fileExists('file.txt')) {\n    $checksum = $fs->checksum('file.txt');\n} else {\n    // write first or report missing file\n}","handlingStrategy":"try-catch","validationCode":"if ( ! $filesystem->fileExists($path)) {\n    throw new DomainException(\"Cannot checksum missing file: {$path}\");\n}\n// Also guard the algorithm name: only FilesystemException is caught in the trait,\n// so an unknown algo (hash_init ValueError) escapes uncaught.\nif ( ! in_array($algo, hash_algos(), true)) {\n    throw new InvalidArgumentException(\"Unknown hash algorithm: {$algo}\");\n}","typeGuard":null,"tryCatchPattern":"use League\\Flysystem\\UnableToProvideChecksum;\n\ntry {\n    $checksum = $filesystem->checksum($path, ['checksum_algo' => $algo]);\n} catch (UnableToProvideChecksum $e) {\n    $previous = $e->getPrevious(); // original FilesystemException, usually UnableToReadFile\n    if ($previous instanceof League\\Flysystem\\UnableToReadFile) {\n        // file missing or unreadable: recover explicitly\n        return $regenerateArtifact($path);\n    }\n    throw $e;\n}","preventionTips":["Validate the algorithm against hash_algos() before calling checksum().","fileExists() before checksum() when existence is not guaranteed.","When wrapping adapters with decorators (PathPrefixedAdapter, ReadOnly), verify checksums still route correctly — the trait is the fallback path."],"tags":["php","flysystem","checksum","stream","fallback","not-found"],"backgroundTag":"checksum-retrieval-failed","analyzedSha":"b277b5dc3d56650b68904117124e79c851e12376","analyzedAt":"2026-08-17T04:28:35.741Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}