Skip to content

Core\Request\Part::peek

$part->peek(uint $max): tainted bytes$part->peek(
    uint $max,
): tainted bytes

Returns the first bytes of one uploaded file and keeps them for later reads. After peek, readAll(), content() and saveTo() still give the whole file, from the first byte. Use it to check the start of a file before you save or read it.

$max is the largest number of bytes to return. A file shorter than $max is returned whole. A second call with a larger $max reads further into the file. The bytes stay in memory until the Core\Request::files() loop moves to the next file. A $max larger than the [limits] memory setting of the request throws a LogicError. Call peek before readAll(), content() or saveTo() start to read the file. After that, and after the loop moves on, it throws a LogicError.

The examples below check the first line of a CSV file, the first bytes of a file in a program's own format, and a text file for a zero byte.

Parameters

  • $maxuintThe most bytes to return. A part shorter than max is returned whole. A second call with a larger max reads further.

Return value

Returns tainted bytes. The first max bytes of the part, or fewer. The value is tainted.

Errors

  • LogicErrorThis program is not handling a request. Or the files() loop has moved to a later part. Or content(), readAll() or saveTo() has already started to read this part. Or max is larger than this request's [limits] memory.
  • 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

Checking the header of a csv upload

<?nvs
// Shows how an expense report website checks the first line of a CSV file before it saves the file.
bytes $header = 'date,amount,note' as bytes;
string $folder = Core\IO::createTemporaryDirectory();
foreach (Core\Request::files() as Core\Request\Part $part) {
// `peek` returns the first 16 bytes of the file. The file is not read yet.
tainted bytes $start = $part->peek(16);
if (!Core\Bytes::startsWith($start, $header)) {
echo $part->filename(), ": not an expense sheet\n";
continue;
}
// `saveTo` still writes the whole file, including the first 16 bytes.
// This prints "march.csv: saved 63 bytes".
string $path = Core\Path::join($folder, 'expenses.csv');
$part->saveTo($path);
echo $part->filename(), ': saved ', Core\IO::size($path), " bytes\n";
}
Output
march.csv: saved 63 bytes
contacts.csv: not an expense sheet

A file of your own format

<?nvs
// Shows how a drawing website checks that an upload is a sketch file in a version it can read.
// Every sketch file starts with the 4 bytes "SKCH" and then one byte for the version.
bytes $magic = 'SKCH' as bytes;
bytes $version2 = Core\Bytes::join([$magic, Core\Encoding::fromHex('02')]);
foreach (Core\Request::files() as Core\Request\Part $part) {
// `peek(5)` returns the first 5 bytes. The rest of the file is not read yet.
tainted bytes $start = $part->peek(5);
if (!Core\Bytes::startsWith($start, $magic)) {
echo $part->filename(), ": not a sketch file\n";
continue;
}
if (!Core\Bytes::startsWith($start, $version2)) {
echo $part->filename(), ": an old version, not supported\n";
continue;
}
// `readAll` still returns the whole file. This prints "logo.skch: 24 bytes".
tainted bytes $sketch = $part->readAll({ max: 1000000 });
echo $part->filename(), ': ', Core\Bytes::length($sketch), " bytes\n";
}
Output
logo.skch: 24 bytes
house.skch: an old version, not supported
cat.skch: not a sketch file

Rejecting a text file that is not text

<?nvs
// Shows how a notes website rejects a text upload that contains a zero byte.
bytes $zero = Core\Encoding::fromHex('00');
foreach (Core\Request::files() as Core\Request\Part $part) {
// A text file never contains a byte with the value 0. Most binary files do.
// `peek(512)` returns the first 512 bytes, or the whole file if it is shorter.
tainted bytes $start = $part->peek(512);
if (Core\Bytes::contains($start, $zero)) {
echo $part->filename(), ": not a text file\n";
continue;
}
// `readAll` still returns the whole file, so this prints all of the note.
tainted bytes $note = $part->readAll({ max: 100000 });
echo $part->filename(), ":\n";
echo Core\Encoding::decodeText($note, Core\Charset::Utf8);
}
Output
meeting.txt:
Meeting at 10.
Bring the floor plan.
summary.txt: not a text file