Core\Request\Part::content
$part->content(): Core\Request\PartContentReturns the content of one uploaded file in pieces. A foreach loop over the result gives one piece at a time, and each piece is tainted bytes. The program holds only the current piece, so a large file never has to fit in memory. A file with no content gives no pieces.
Read the content while the Core\Request::files() loop is on this file. When the loop has moved to the next file, the content of this one is gone, and reading it throws a LogicError. You can stop early with break. The loop over the files then skips the rest of this file.
Use readAll when the file is small enough to hold as one value, and saveTo to write it to a file.
The examples show how to measure each file, how to stop at a size limit, and how to compute a checksum while the file arrives.
Return value
Returns Core\Request\PartContent. An Iterable<tainted bytes> over the pieces of the part as they arrive. Each piece is a value of its own. The loop runs zero times for a part with no bytes. It works only while the files() loop is still on this part.
Errors
LogicErrorThis program is not handling a request. Or thefiles()loop has moved to a later part, and the bytes of this part are gone.IOErrorThe connection failed while the body was read, or the body ended before the closing boundary.ParseErrorThe body is not the multipart body the request declared.
Examples
The size of each file
<?nvs// Shows how to read an uploaded file in pieces.
foreach (Core\Request::files() as Core\Request\Part $part) { // `content` gives the file one piece at a time. Each piece is `tainted bytes`. uint $size = 0; foreach ($part->content() as tainted bytes $chunk) { $size = $size + Core\Bytes::length($chunk); } // The first file prints "invoice.pdf: 24 bytes". echo $part->filename(), ': ', $size, " bytes\n";}invoice.pdf: 24 bytesreceipt.txt: 24 bytesStopping at a size limit
<?nvs// Shows how to stop reading an uploaded file when it is too large.
uint $limit = 32;foreach (Core\Request::files() as Core\Request\Part $part) { uint $size = 0; foreach ($part->content() as tainted bytes $chunk) { $size = $size + Core\Bytes::length($chunk); // `break` stops reading this file. The next file can still be read. if ($size > $limit) { break; } } if ($size > $limit) { echo $part->filename(), ': larger than ', $limit, " bytes\n"; } else { echo $part->filename(), ': ', $size, " bytes\n"; }}long-letter.txt: larger than 32 bytesnote.txt: 18 bytesA checksum of each document
<?nvs// Shows how a document service computes the checksum of each uploaded file.// The program never has a whole file in memory at once.
foreach (Core\Request::files() as Core\Request\Part $part) { // `Core\Hash::stream` starts a SHA-256 digest that you can feed piece by piece. Core\Hash\Stream $digest = Core\Hash::stream(Core\Hash::Digest::Sha256); foreach ($part->content() as tainted bytes $chunk) { $digest->update($chunk); } // `finish` returns the digest of every byte of this file. echo $part->filename(), ' ', Core\Encoding::toHex($digest->finish()), "\n";}contract.txt d234b0dcf57e332afd429779c6d8a29beaca067d6f85976eee047300ae456bd1terms.txt 408d66bfb41a5a1f35c4cb8757e854c63007f2a045fe7a1b2becf6bf4d90551f