Jackdaw's Nest A Web Log

[ Log ] [ Projects ] [ About ]

File Upload in Silex 2015/10/19

For some reason I never remember how to save an uploaded file (or the contents of the file, to be specific) in Silex, my favourite Symfony2 based framework. Well, here it is. I hope I'll remember it if I write it down.

// Get the /Symfony\Component\HttpFoundation\File\UploadedFile
// 'file' is the name of the upload file field in the form.
$file = $request->files->get('file');

// Get the contents of the file. $file->getRealPath() returns
// the full path to the temporarily saved file.
$content = file_get_contents($file->getRealPath());

There is a move method in /Symfony\Component\HttpFoundation\File\UploadedFile but I rarely use it. I usually use Flysystem to abstract the filesystem away.