Skip to content

Instantly share code, notes, and snippets.

@Zegnat Zegnat/example.php
Created Apr 24, 2018

What would you like to do?
Example of using a PSR-7 library to parse an incoming request that contains one or more `photo` uploads.
use \Nyholm\Psr7\Factory\ServerRequestFactory;
// Use the (draft) PSR-17 Factory to create our PSR-7 Request object:
$incomingRequest = ServerRequestFactory::createServerRequestFromGlobals();
// Check for uploaded files on the Request object.
// @see
$files = $incomingRequest->getUploadedFiles();
if (isset($files['photo'])) {
// Sadly PSR-7 does not normalise single files v.s. multiple files.
// If it is a single file, still wrap it in an array so we can loop later:
if (!is_array($files['photo'])) {
$files['photo'] = [ $files['photo'] ];
foreach ($files['photo'] as $photo) {
// Here $photo is a standardised object per PSR-7 UploadedFileInterface
// @see
// Lets use the PSR given example code to save it to disk:
$filename = sprintf(
pathinfo($photo->getClientFilename(), PATHINFO_EXTENSION)
$photo->moveTo('photos/' . $filename);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.