How to Upload Multiple Files to pdfRest with PHP
This step-by-step guide demonstrates how to upload multiple files to pdfRest in one multipart request from a PHP application through the Upload Files API Tool. Along the way, we'll distinguish the PHP syntax from the Upload Multiple to pdfRest API contract and show how its returned data supports the next step.
Why Upload Multiple Files to pdfRest with PHP?
Document assembly and batch processing often require several inputs before the next operation can begin. Upload Files can stage those resources together and return one response describing every accepted file.
A PHP claims application might upload a signed form, supporting photograph, and spreadsheet in one call. It can preserve the filename-to-ID mapping and then choose the correct resource for attachment, conversion, or merging steps.
Each file is represented by a separate multipart part with the same file field name. Repeating the field is intentional; sending one comma-separated path would not create three uploaded resources.
PHP Code Example
require 'vendor/autoload.php'; // Require the autoload file to load Guzzle HTTP client.
use GuzzleHttp\Client; // Import the Guzzle HTTP client namespace.
use GuzzleHttp\Psr7\Request; // Import the PSR-7 Request class.
use GuzzleHttp\Psr7\Utils; // Import the PSR-7 Utils class for working with streams.
// By default, we use the US-based API service. This is the primary endpoint for global use.
$apiUrl = "https://api.pdfrest.com";
/* For GDPR compliance and enhanced performance for European users, you can switch to the EU-based service by uncommenting the URL below.
* For more information visit https://pdfrest.com/pricing#how-do-eu-gdpr-api-calls-work
*/
//$apiUrl = "https://eu-api.pdfrest.com";
$client = new Client(); // Create a new instance of the Guzzle HTTP client.
$headers = [
'Api-Key' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' // Set the API key in the headers for authentication.
];
$options = [
'multipart' => [
[
'name' => 'file', // Specify the field name for the first file.
'contents' => Utils::tryFopen('/path/to/file1', 'r'), // Open the first file for reading.
'filename' => '/path/to/file1', // Set the filename for the first file to be uploaded.
'headers' => [
'Content-Type' => '' // Set the Content-Type header for the first file.
]
],
[
'name' => 'file', // Specify the field name for the second file.
'contents' => Utils::tryFopen('/path/to/file2', 'r'), // Open the second file for reading.
'filename' => '/path/to/file2', // Set the filename for the second file to be uploaded.
'headers' => [
'Content-Type' => '' // Set the Content-Type header for the second file.
]
],
[
'name' => 'file', // Specify the field name for the third file.
'contents' => Utils::tryFopen('/path/to/file3', 'r'), // Open the third file for reading.
'filename' => '/path/to/file3', // Set the filename for the third file to be uploaded.
'headers' => [
'Content-Type' => '' // Set the Content-Type header for the third file.
]
]
]
];
$request = new Request('POST', $apiUrl.'/upload', $headers); // Create a new HTTP POST request with the API endpoint and headers.
$res = $client->sendAsync($request, $options)->wait(); // Send the asynchronous request and wait for the response.
echo $res->getBody(); // Output the response body.
Source for Upload Multiple to pdfRest: View the PHP sample on GitHub.
Breaking Down the Code
Load Guzzle and its stream helpers
require 'vendor/autoload.php'; // Require the autoload file to load Guzzle HTTP client. use GuzzleHttp\Client; // Import the Guzzle HTTP client namespace. use GuzzleHttp\Psr7\Request; // Import the PSR-7 Request class. use GuzzleHttp\Psr7\Utils; // Import the PSR-7 Utils class for working with streams.
Guzzle supplies the HTTP client and PSR-7 request objects used throughout the Upload Multiple to pdfRest example. For this PHP Upload Multiple to pdfRest flow, Utils::tryFopen creates readable multipart streams without loading every uploaded byte into one string.
Select the pdfRest service region
// By default, we use the US-based API service. This is the primary endpoint for global use. $apiUrl = "https://api.pdfrest.com"; /* For GDPR compliance and enhanced performance for European users, you can switch to the EU-based service by uncommenting the URL below. * For more information visit https://pdfrest.com/pricing#how-do-eu-gdpr-api-calls-work */ //$apiUrl = "https://eu-api.pdfrest.com"; $client = new Client(); // Create a new instance of the Guzzle HTTP client.
The active $apiUrl selects the US service for Upload Multiple to pdfRest; the commented assignment shows the EU alternative. Keep the PHP uploads, request IDs, and generated resources for Upload Multiple to pdfRest on that same regional host.
Add each file as a repeated multipart part
$options = [
'multipart' => [
[
'name' => 'file', // Specify the field name for the first file.
'contents' => Utils::tryFopen('/path/to/file1', 'r'), // Open the first file for reading.
'filename' => '/path/to/file1', // Set the filename for the first file to be uploaded.
'headers' => [
'Content-Type' => '' // Set the Content-Type header for the first file.
]
],
[
'name' => 'file', // Specify the field name for the second file.
'contents' => Utils::tryFopen('/path/to/file2', 'r'), // Open the second file for reading.
'filename' => '/path/to/file2', // Set the filename for the second file to be uploaded.
'headers' => [
'Content-Type' => '' // Set the Content-Type header for the second file.
]
],
[
'name' => 'file', // Specify the field name for the third file.
'contents' => Utils::tryFopen('/path/to/file3', 'r'), // Open the third file for reading.
'filename' => '/path/to/file3', // Set the filename for the third file to be uploaded.
'headers' => [
'Content-Type' => '' // Set the Content-Type header for the third file.
]
]
]
];
$request = new Request('POST', $apiUrl.'/upload', $headers); // Create a new HTTP POST request with the API endpoint and headers.
$res = $client->sendAsync($request, $options)->wait(); // Send the asynchronous request and wait for the response.
echo $res->getBody(); // Output the response body.
Each local source is added by PHP under another file part in the same multipart body. The Upload Multiple to pdfRest response returns several resources whose filenames and IDs the PHP caller should keep paired.
Send the multipart upload
$request = new Request('POST', $apiUrl.'/upload', $headers); // Create a new HTTP POST request with the API endpoint and headers.
$res = $client->sendAsync($request, $options)->wait(); // Send the asynchronous request and wait for the response.
echo $res->getBody(); // Output the response body.
The PHP request joins the Upload Multiple to pdfRest endpoint, authentication header, and prepared body. For this Upload Multiple to pdfRest call, the multipart metadata generated by PHP must remain paired with the body so pdfRest can separate files from options.
Beyond the Tutorial
You now know how PHP can send several files in one multipart body and preserve the resource information returned for each upload.
Confirm that the response contains every expected input and store each ID beside its filename rather than relying only on array position. Partial or reordered application data should not cause a later workflow to process the wrong document.
Try the Upload Multiple to pdfRest workflow with a representative file in API Lab, then adapt the request in PHP. The Upload Files API Tool documentation provides the complete PHP context for Upload Multiple to pdfRest values, defaults, and limitations.