Files
php-curl-class/examples/receive_large_file_chunked.php
T
Zach Borboa a79e21b0d1 Fix PHP_CodeSniffer coding standard errors (#993)
* Fix PHP_CodeSniffer errors

Error message:
Header blocks must be separated by a single blank line (PSR12.Files.FileHeader.SpacingAfterTagBlock)

* Fix PHP_CodeSniffer errors

Error message:
Opening brace should be on a new line (Squiz.Functions.MultiLineFunctionDeclaration.BraceOnSameLine)

* Fix PHP_CodeSniffer warnings

Error message:
A file should declare new symbols (classes, functions, constants, etc.)
and cause no other side effects, or it should execute logic with side
effects, but should not do both. The first symbol is defined on line 6
and the first side effect is on line 14.
(PSR1.Files.SideEffects.FoundWithSymbols)
2025-09-15 21:24:22 -04:00

24 lines
733 B
PHP

<?php
// Receive PUT file.
// See also "examples/put_large_file_chunked.php".
function file_get_contents_chunked($filename, $chunk_size, $callback) {
$handle = fopen($filename, 'r');
while (!feof($handle)) {
call_user_func_array($callback, [fread($handle, $chunk_size)]);
}
fclose($handle);
}
$tmpnam = tempnam('/tmp', 'php-curl-class.');
$file = fopen($tmpnam, 'wb+');
// Use file_get_contents_chunked() rather than file_get_contents() to avoid error:
// "Fatal error: Allowed memory size of ... bytes exhausted (tried to allocate ... bytes) in ... on line 0".
file_get_contents_chunked('php://input', 4096, function ($chunk) use (&$file) {
fwrite($file, $chunk);
});
// @codingStandardsIgnoreFile