Core\Request\Part::readAll
Returns the whole content of one uploaded file as a single tainted bytes value. Use it for a file that is small enough to keep in memory, such as a picture, an invoice or a short document. An empty file gives empty bytes.
Without an argument, the file may be as large as the [limits] request_body setting, which is 8 MB by default. { max: … } sets your own largest size in bytes. A file larger than the limit throws a RuntimeError. A max larger than the [limits] memory setting of the request throws a LogicError. Call readAll while the Core\Request::files() loop is on this file. After the loop moves on, it throws a LogicError. The content can be read only once, so a second call returns empty bytes.
Use content() to read a large file in pieces, and saveTo to write it to a file. Some methods read the upload directly, such as Core\Mime::detect and Core\Hash::of. They need no readAll.
The examples show how to print a small text file, how to set a size limit, and how to send a PDF upload to another service as Base64.
Options
maxuintoptionalThe most bytes this call reads into memory. If you leave it out, the limit is[limits] request_body. If you give it, the limit is this number. A number larger than the request's own[limits] memorythrows an error. It is not lowered to fit.
Return value
Returns tainted bytes. Every byte of this part, in order and not decoded. The value is tainted. It is empty for a part with no bytes. That part was still sent by the client.
Errors
LogicErrorThis program is not handling a request. Or thefiles()loop has moved to a later part. Ormaxis larger than this request may use at all.RuntimeErrorThe part is larger than the limit. The bytes over the limit are never stored, because the error is thrown at the piece that would pass it. Usecontent()for a part that does not fit.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
Reading a short note
<?nvs// Shows how to read a small uploaded file as one value and print it as text.
foreach (Core\Request::files() as Core\Request\Part $part) { // `readAll` returns the whole file as `tainted bytes`. tainted bytes $content = $part->readAll(); echo $part->filename(), ': ', Core\Bytes::length($content), " bytes\n"; // `Core\Encoding::decodeText` reads the bytes as UTF-8 text. echo Core\Encoding::decodeText($content, Core\Charset::Utf8), "\n";}shopping.txt: 27 bytesBread, milk and two lemons.A file larger than max
<?nvs// Shows how to set the largest file size that `readAll` accepts.
foreach (Core\Request::files() as Core\Request\Part $part) { try { // `max` is the largest size in bytes. A larger file throws a `RuntimeError`. tainted bytes $content = $part->readAll({ max: 40 }); echo $part->filename(), ': ', Core\Bytes::length($content), " bytes\n"; } catch (RuntimeError $tooLarge) { // The next file in the loop can still be read. echo $part->filename(), ": larger than 40 bytes\n"; }}story.txt: larger than 40 bytesthanks.txt: 29 bytesAccepting only pdf invoices
<?nvs// Shows how an accounting website sends each uploaded PDF invoice to its bookkeeping service.
foreach (Core\Request::files() as Core\Request\Part $part) { // The client chooses the file name, so the program checks the content. // `Core\Mime::detect` reads only the first bytes. The program can still read the whole file after it. if (Core\Mime::detect($part) != Core\Mime::Type::Pdf) { echo $part->filename(), ": not a PDF file\n"; continue; } // The bookkeeping service needs the whole file as Base64 text, so the program reads all of it. // A file larger than 5000000 bytes throws a `RuntimeError`. tainted bytes $content = $part->readAll({ max: 5000000 }); tainted string $encoded = Core\Encoding::toBase64($content); echo $part->filename(), ': ', Core\Bytes::length($content), ' bytes, sent as '; echo Core\Str::length($encoded), " characters of Base64\n";}invoice-march.pdf: 52 bytes, sent as 72 characters of Base64invoice-april.pdf: not a PDF file