PSR-7 Standard – Part 4 – File Uploads

PSR-7 Standard – Part 4 – File Uploads

This post is part of series:

After we learned what a Request and a Response are, let’s now look how we can send files to the server. Then have a look on how we can process them with Guzzle on the server side.

Client Side Script

As you can see in the diagram, a file upload is also handled as stream.

First we create script “file_upload.php” with this content which initializes the autoloader and creates a sample file for the upload test.

After we have a text file, we can create our stream with the Guzzle PSR-7 component. Please add the code to the existing file.

The MultipartStream gives us the ability to send more than once file to the server. As last part of the script we need to create a Request to send the MultipartStream to the server.

Server Side Script

Please create the script “server_file.php” to receive the files.

The script is very simple. It creates a ServerRequest (we talk about this in a future blog post). The ServerRequest can handle the MultipartStream and return the files with the handy method method getUploadedFiles. This methods is now a standard to get all data of the uploaded files. If you know how files are handled without PSR-7 you know that this is a really enhancement.

For debugging we collect all data of the uploaded files and return them back as response to the client.

Test the Upload

Run the server in console:

Run the client script in a second console:

If you did everything correct you should now see the result of the server script:


That’s it. The next blog post will give you some inside into the HTTP Client.

Leave a Reply

%d bloggers like this: