Skip to content

Core\Request::files

Returns the files that a form uploaded. A foreach loop reads them one at a time. Each file is a Core\Request\Part with the form field name, the file name that the client sent, the declared type and the content.

Read the content while the loop is on that file. readAll returns it as one value, content returns it in pieces, and saveTo writes it to a file. When you skip a file, the loop reads and discards its content. The file name and the type are tainted, which means they came from outside your program.

The text fields of the form are not in the loop. Read them with Core\Request::formField after the loop. When the request has no multipart/form-data body, the loop runs zero times.

The body can be read only once in this way. After files, the methods body, bytes, bodyStream and json throw a LogicError. A command-line program answers no request, so files throws a LogicError there.

Return value

Returns Core\Request\Files. An Iterable<Core\Request\Part>. Each loop step gives the next file as it arrives. The text fields are not in the loop, and formField reads them after it. The loop runs zero times when the request has no multipart/form-data body.

Errors

  • LogicErrorThe program is not handling a request. Or body, bodyStream or formField already read the body. Once files is called, body, bytes, bodyStream and json throw this error too. You can still call formField after the loop.
  • ParseErrorThe request says it has a multipart/form-data body, but its boundary is missing, given twice or not valid. Or the body does not have the form that the request declared. Or the name, file name or Content-Type of a part is not valid UTF-8.
  • IOErrorThe connection failed while the loop read the body, or the body ended before its last boundary.

Examples

Listing the uploaded files

<?nvs
// Shows how to list the files that a form uploaded.
// Each loop step gives one uploaded file as a `Core\Request\Part`.
// `readAll` returns the content of the file.
foreach (Core\Request::files() as Core\Request\Part $part) {
tainted bytes $content = $part->readAll();
echo $part->name(), ': ', $part->filename(), ', ', $part->contentType();
echo ', ', Core\Bytes::length($content), " bytes\n";
}
Output
photo: beach.jpg, image/jpeg, 16 bytes
notes: trip.txt, text/plain, 20 bytes

A form with files and text fields

<?nvs
// Shows how to read a form that has a file and text fields.
// `files` gives only the parts that are files. The text fields are skipped here.
foreach (Core\Request::files() as Core\Request\Part $part) {
echo 'file: ', $part->filename(), "\n";
}
// `Core\Request::formField` reads the text fields. Call it after the loop.
// This prints "Sales in March" and "Ada".
echo 'title: ', Core\Request::formField('title') ?? '', "\n";
echo 'author: ', Core\Request::formField('author') ?? '', "\n";
Output
file: march.csv
title: Sales in March
author: Ada

Checking an avatar before it is stored

<?nvs
// Shows how a website checks an uploaded profile picture before it stores it.
use Novis\Image\Format;
use Novis\Image\Image;
string $folder = Core\IO::createTemporaryDirectory();
foreach (Core\Request::files() as Core\Request\Part $part) {
// The form has one file field, `avatar`. Other files are skipped.
if ($part->name() != 'avatar') {
continue;
}
try {
// `Image::info` reads the format and the size from the upload. It does not decode the pixels.
// A file that is not an image throws a `ParseError`.
var $info = Image::info($part);
if ($info->format != Format::Png && $info->format != Format::Jpeg) {
echo "only PNG and JPEG pictures are allowed\n";
continue;
}
if ($info->width > 1000 || $info->height > 1000) {
echo "the picture is larger than 1000 by 1000 pixels\n";
continue;
}
// The client chose the file name "../../me.png", so the program does not use it.
string $path = Core\Path::join($folder, 'avatar-1042.' . Image::extension($info->format));
// `saveTo` still writes the whole file, because `Image::info` did not use it up.
// `max` is the largest size in bytes. A larger file throws a `RuntimeError`.
$part->saveTo($path, { max: 2000000 });
echo 'stored a ', $info->width, ' by ', $info->height, ' picture of ', Core\IO::size($path), " bytes\n";
} catch (ParseError $notAnImage) {
echo "the file is not a picture\n";
} catch (RuntimeError $tooLarge) {
echo "the picture is larger than 2 MB\n";
}
}
Output
stored a 96 by 96 picture of 542 bytes