Skip to content

Instantly share code, notes, and snippets.

@danharper
Created January 27, 2015 08:15
Show Gist options
  • Save danharper/0829269d6aa8b3180ea9 to your computer and use it in GitHub Desktop.
Save danharper/0829269d6aa8b3180ea9 to your computer and use it in GitHub Desktop.
Symfony UploadedFile methods with FileBag notes

Three file inputs, last being "multiple". Let's say we upload a file on each, and three on the multiple input (upload3).

<input type="file" name="upload1">
<input type="file" name="upload2">
<input type="file" name="upload3[]" multiple> <!-- note the [] on name to indicate array -->

Getting files off Request:

/** @var Symfony\Component\HttpFoundation\Request $request */
$request->files; // Symfony\Component\HttpFoundation\FileBag

FileBag is iterable. Iterated, looks like:

[
  'upload1' => Symfony\Component\HttpFoundation\File\UploadedFile,
  'upload2' => Symfony\Component\HttpFoundation\File\UploadedFile,
  'upload3' => [
    Symfony\Component\HttpFoundation\File\UploadedFile,
    Symfony\Component\HttpFoundation\File\UploadedFile,
    Symfony\Component\HttpFoundation\File\UploadedFile,
  ]
]

Getting info from an UploadedFile:

print_r([
	'getClientOriginalName' => $file->getClientOriginalName(),
	'getFilename' => $file->getFilename(),
	'getClientOriginalExtension' => $file->getClientOriginalExtension(),
	'getExtension' => $file->getExtension(),
	'guessExtension' => $file->guessExtension(),
	'getClientMimeType' => $file->getClientMimeType(),
	'getMimeType' => $file->getMimeType(),
	'getPath' => $file->getPath(),
	'getPathname' => $file->getPathname(),
	'getRealPath' => $file->getRealPath(),
]);

Ran against an uploaded file named composer.json, gives us:

Array
(
  [getClientOriginalName] => composer.json
  [getFilename] => phpdLcSXY
  [getClientOriginalExtension] => json
  [getExtension] => 
  [guessExtension] => txt
  [getClientMimeType] => application/json
  [getMimeType] => text/plain
  [getPath] => /private/var/folders/w3/5zrdfsn55w3d_lwtf7ry2wn00000gn/T
  [getPathname] => /private/var/folders/w3/5zrdfsn55w3d_lwtf7ry2wn00000gn/T/phpdLcSXY
  [getRealPath] => /private/var/folders/w3/5zrdfsn55w3d_lwtf7ry2wn00000gn/T/phpdLcSXY
)
@foozee
Copy link

foozee commented Oct 7, 2015

Thanks a lot, you saved me 😄 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment