Skip to content
Advertisement

$_FILES key used for building a PSR-7 uploaded files list

The short version:

When a user uploads a file using a form, an array is saved in the global variable $_FILES. For example, when using:

JavaScript

the global variable looks like this:

JavaScript

In principle, I need to know which of the keys of the array $_FILES['myfiles0'] always exists and (maybe) is always set, no matter how the other keys look like, or which browser is used. Could you please tell me?

Please take into the consideration, that the $_FILES variable can also contain multi-dimensional arrays for files uploaded using an array notation, like this:

JavaScript

The long version:

For my implemention of PSR-7 Uploaded files I need to do the normalization of the uploaded files list. The initial list can be provided by the user, or can be the result of a standard file upload using a form, e.g. the $_FILES global variable. For the normalization process I need to check for the existence and “correctness” (maybe a poor choice of the word) of one of the following standard file upload keys:

  • name
  • type
  • tmp_name
  • error
  • size

In principle, if, in the provided uploaded files list (which can be a multi-dimensional array as well), the chosen key (I choosed tmp_name for now) is found, then it will be supposed that the array item to which the key belongs is a standard file upload array item, containing the above key list. Otherwise, e.g. if the chosen key is not found, it will be supposed that the corresponding array item is an instance of UploadedFileInterface.

Unfortunately, in case of a standard file upload, I can’t find nowhere a solide information about which key (from the above list) always exists and (maybe) is always set in the $_FILES variable, no matter how the other list keys look like, or which browser is used.

I would appreciate, if you could help me in this matter.

Thank you.

Advertisement

Answer

I decided to use the tmp_name key for the file(s) upload validation.

Unfortunately I’ve taken this decision a long time ago. So I can’t remember anymore all arguments supporting it, resulted from the documentations I read and the tests I performed. Though, one of the arguments has been, that, in comparison with other key(s), the value of the tmp_name key can’t be set/changed on the client-side. The environment on which the application is running decides which value should be set for it.

I’ll post here the final version of the PSR-7 & PSR-17 implementation (regarding uploaded files) that I wrote back then. Maybe it will be helpful for someone.


The implementation of ServerRequestFactoryInterface:

It reads the list of uploaded files (found in $_FILES, or manually passed as argument) and, if not already done, transforms it to “a normalized tree of upload metadata, with each leaf an instance of PsrHttpMessageUploadedFileInterface” (see “1.6 Uploaded files” in PSR-7).

Then it creates a ServerRequestInterface instance, passing it the normalized list of uploaded files.

JavaScript

The base class ServerRequestFactory:

JavaScript

Creating the ServerRequestInterface instance by the ServerRequestFactoryInterface implementation:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement