Question

How to get Serverless PHP uploaded file

I am trying to write a simple PHP serverless function (so simple I’m just writing it in the web UI).

I am POSTing form-data containing some text values and a file. The function will just take the file and upload it to a DO Space.

The $_FILES variable is empty and I can’t find where the uploaded file is, or if it even exists. I can’t find any reference to it in the function args parameter either.

Do these function support file uploads?

Show comments

Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Should we also look in the $event array for HTTP POST body/payload? I need to parse JSON sent in the body.

Matt Welke
DigitalOcean Employee
DigitalOcean Employee badge
March 21, 2023

In PHP, when used with web servers, the $_FILES variable is filled in with info about the file specified in the web form and saved on the disk by the web server. With our FaaS product Functions, things work differently. Instead of the variables $_FILES, $_GET, and $_POST, we make as much data about the request as we can available to you by including it in the “event” parameter of your function.

Unrelated to your use case, but related to the function signature, is the fact that we also make as much data about the execution context as we can available to you by including it in the “context” parameter of your function.

So in the end, a full featured PHP function signature is as follows:

<?php

function main(array $event, object $context): array
{
    $name = $args["name"] ?? "stranger";
    $greeting = "Hello $name!";
    echo $greeting;
    return [
        "body" => [
            "event" => $event,
            "context" => $context,
        ],
    ];
}

This example returns the complete event parameter and context parameter in the response body. This helps you see all the data that is available to you while you process the request.

For your use case, try using an HTTP client, such as the web browser you described in your question, or curl, to send a request where you’d normally see file data in $_FILES if it were PHP used with a web server. You’ll see how our system processes this and makes it available in the event parameter.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

card icon
Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Sign up
card icon
Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We’d like to help.

Learn more
card icon
Become a contributor

You get paid; we donate to tech nonprofits.

Learn more
Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow – whether you’re running one virtual machine or ten thousand.

Learn more ->
DigitalOcean Cloud Control Panel