Skip to content

Core\Http\Part::file

Core\Http\Part::file(
    string $path,
    {filename?: string, contentType?: string},
): Core\Http\Part

Sends a file as the body of a request, or as one field of a form upload, without loading the whole file into memory first.

Core\Http\Part::file does not read the file. It checks that the file exists and returns a part that contains the path. The file is read while the request is being sent, so a large upload uses little memory. You give the part to body, or to one field of multipart. The other server sees the last part of the path as the file name, and filename changes it. contentType sets the media type.

A program you start from the command line may send any file. On a server, a program may send the files in its application folder, except nvs.toml and names that start with .. Any other path needs the fs.read capability in [capabilities.fs] in nvs.toml. Without it, the call throws a RuntimeError on the line that names the file. If there is no file at the path, it throws an IOError.

Good to know: the file is read when the request is sent. If the file changes before that, the new content is sent.

Parameters

  • $pathstringThe file to send. A relative path must be a string written directly in the code. It is joined to the folder of the file that contains it.

Options

  • filenamestringoptionalThe file name sent to the server. By default, it is the last part of the path.
  • contentTypestringoptionalThe media type of this part. If it is not given, a multipart field is sent as application/octet-stream. A whole body uses the call's own contentType.

Return value

Returns Core\Http\Part. A part. Pass it as body or as one key of multipart.

Errors

  • RuntimeErrorThe configuration does not grant fs.read for this path.
  • IOErrorNothing exists at $path, or the operating system did not return its details.

Examples

The examples below run with this configuration file.

nvs.toml
# The program may read files in the `data` directory beside this file, and no other file.
[capabilities.fs]
read = ["data"]
# The program may connect to public hosts only.
# `internal = []` allows no address on this computer or on a private network.
[capabilities.net]
internal = []

A file as the whole body

<?nvs
// Shows how to send a file as the whole body of a request.
// `Core\Http\Part::file` checks that the file exists and returns a part.
// It does not read the file yet. The path starts at the folder of this file.
Core\Http\Part $invoice = Core\Http\Part::file('data/invoice-1042.txt', { contentType: 'text/plain' });
echo "part created\n";
// The file is read while the request is sent. 10.0.0.7 is a private address,
// so the request throws a `RuntimeError` before anything is sent.
try {
Core\Http\Response $response = Core\Http\Client::put('https://10.0.0.7/invoices/1042', { body: $invoice });
echo 'status: ', $response->status(), "\n";
} catch (RuntimeError $refused) {
echo $refused->message, "\n";
}
Output
part created
Core\Http\Client::put cannot connect to 10.0.0.7: it is private (10/8, 172.16/12, 192.168/16), which `net.internal` does not except
help: add `internal = ["10.0.0.7"]` under `[capabilities.net]` in nvs.toml

A missing file and a file you may not read

<?nvs
// Shows the two errors `Core\Http\Part::file` throws, on the line that names the file.
// There is no file with this name, so the call throws an `IOError`.
try {
Core\Http\Part $missing = Core\Http\Part::file('data/invoice-9999.txt');
echo "part created\n";
} catch (IOError $notThere) {
echo "invoice-9999.txt: the file does not exist\n";
}
// This program may only read files in the `data` directory. `nvs.toml` is outside it,
// so the call throws a `RuntimeError`.
try {
Core\Http\Part $config = Core\Http\Part::file('nvs.toml');
echo "part created\n";
} catch (RuntimeError $denied) {
// The message names the full path of the file. This prints the part before it.
echo Core\Str::before($denied->message, ' for '), "\n";
}
Output
invoice-9999.txt: the file does not exist
Core\Http\Part::file needs the capability `fs.read`

Uploading a monthly report

<?nvs
// Shows a monthly job that uploads a sales report to an accounting service,
// as a form with one text field and one file.
// `filename` is the name the accounting service sees. Without it, the name would be
// `sales-2026-09.csv`, the last part of the path.
Core\Http\Part $file = Core\Http\Part::file('data/sales-2026-09.csv', { filename: 'shop-sales-september.csv', contentType: 'text/csv' });
bool $uploaded = false;
try {
// `multipart` sends a form upload. Each key is one field of the form.
Core\Http\Response $response = Core\Http\Client::post('https://192.168.20.5/api/reports', {
multipart: [
'month' => '2026-09',
'report' => $file,
],
deadline: 60s,
});
$uploaded = $response->status() == 200 || $response->status() == 201;
} catch (RuntimeError $error) {
echo 'not uploaded: ', $error->message, "\n";
}
// 192.168.20.5 is a private address, so the upload fails and this prints `try again tomorrow: yes`.
echo 'try again tomorrow: ', $uploaded ? 'no' : 'yes', "\n";
Output
not uploaded: Core\Http\Client::post cannot connect to 192.168.20.5: it is private (10/8, 172.16/12, 192.168/16), which `net.internal` does not except
help: add `internal = ["192.168.20.5"]` under `[capabilities.net]` in nvs.toml
try again tomorrow: yes