How to get file properties from a post request using Laravel & Slim

11.4k Views Asked by At

I'm using Laravel + Slim to build an application.

I have a form with file upload and I managed successfully to get the information from text inputs:

$request = $app->request;

$title = $request->post('title');

But how I can get the properties like size, name, tmp_name, etc. from a file input?

I found this question and tried like that but I get this error:

Fatal error: Call to undefined method Slim\Http\Request::file()

1

There are 1 best solutions below

1
On BEST ANSWER

There is couple of method you can apply while you uploading some file(s) using laravel's. Here is the couple of them and most important to, in my thought.

$request->hasFile('file_name')

hasFile method to check user upload file or not.

$request->file('file_name')->isValid()

isValid method take care to check file have no error.

After these checks, you have to get file properties and move to desired location. For these purposes.

$document = $request->file('file_name');

Get the document information object. So after that you can use further helper function provided my laravel FileUploader.

$document->getRealPath();
$document->getClientOriginalName();
$document->getClientOriginalExtension();
$document->getSize();
$document->getMimeType();

and finally to move the uploaded file

$document->move($destinationPath);
$document->move($destinationPath, $fileName);

update

However if you using slim to upload file, you have to use native php methods like globals Files array to get file and move_uploaded_file to move file and like so.

If you still have any question related to upload a file or file validation let me know.