file_base64()
Return the content of a stored file (from a file field) as a base64-encoded string. Use it to send a file's bytes in an HTTP request body.
Syntax
file_base64(file)
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
file |
object | array | yes | A file object {name, path, type, size, host}, or a whole file-field value (an array of file objects — the first is used). |
Returns
The file's content, base64-encoded. Throws if the value isn't a file, the file doesn't belong to your workspace, or it exceeds the 25 MB limit.
Example
// A file field holds an array of files — take the first, or loop them.
$response = curl_post_json("https://api.example.com/upload", [
"filename" => $record["photo"][0]["name"],
"image" => file_base64($record["photo"][0])
]);
Notes
- File fields are arrays.
$record["photo"]is the list;$record["photo"][0]is the first file. Passing the whole array uses the first file. - Limited to files 25 MB or smaller (base64 holds the whole file in memory).
- To embed an image in a generated PDF, use file_data_uri() instead.
See also: file_data_uri(), file_url(), curl_request()