laravel / ideas

Issues board used for Laravel internals discussions.
938 stars 28 forks source link

Suggest a solution to enable files with PUT method (I have an implementation working). #2575

Open muchologo opened 3 years ago

muchologo commented 3 years ago

Hello guys!

I would want to suggest a solution to enable files with PUT method, right now I'm using two files to implement it:

1.- One middleware to append file variables to the request 2.- One class file to serve as a proxy of Symfony\Component\HttpFoundation\File\UploadedFile.

The reason to create a proxy is to change the funtionality of the function isValid, and that is because it uses the function is_uploaded_file which is not compatible with PUT or PATCH methods.

MY TEMPORALLY WORKING SOLUTION

1.- Read the php://input in the middleware looking for file input fields to append them to the request, then store the files to a safe directory which could be the one returned by sys_get_temp_dir.

2.- Then the UploadedFile proxy can validate that the uploaded files are inside that same directory.

A POSSIBLE FINAL SOLUTION

1.- Integrate the middleware to Laravel core so the user doesn't need to create it or download it with composer. 2.- Change the code of the function isValid in the class Symfony\Component\HttpFoundation\File\UploadedFile so it includes an extra validation for PUT and PATCH methods and uses the sys_get_temp_dir approach.

FURTHER EXPLANATION

The middleware which parses the raw request data must considere following rules: 1.- Use a buffer to optimize the memory while reading the file data. 2.- Follow the PHP conventions to build the file arrays for both single and multiple fields. 3.- Once a file is fully read, write it to temp directory to free memory.

FEATURES: Using files with PUT as we usually do with POST such as: $request->has("file") $request->file->storeAs() $request->file->getClientOriginalName()

These files contains the first approach but let me know if you're interested to further develope remaining functionality: Kernel.txt PutPatchFix.txt UploadedFile.txt