Get data from an object


#1

I am having problems to get data from an object using POO in PHP. The object I have is the following (doing var_dump(request->$files) in symfony):

object(Symfony\Component\HttpFoundation\FileBag)#64 (1) {
  ["parameters":protected]=>
  array(1) {
    ["registro"]=>
    array(1) {
      ["imagen"]=>
      object(Symfony\Component\HttpFoundation\File\UploadedFile)#35 (6) {
        ["test":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
        bool(false)
        ["originalName":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
        string(36) "12e846a1d8e3a88c89929600b6bdfb9f.jpg"
        ["mimeType":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
        string(10) "image/jpeg"
        ["error":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
        int(0)
        ["pathName":"SplFileInfo":private]=>
        string(24) "C:\xampp\tmp\php697B.tmp"
        ["fileName":"SplFileInfo":private]=>
        string(11) "php697B.tmp"
      }
    }
  }
}

I have tried with the following:

$image_name = $request->files('imagen');

OR

$image_name = $request->files->get('imagen');


#2

If you use the Symfony forms component then it’s as simple as

$file = $form['imagen']->getData();

Otherwise it’s

$file = $request->files->get('registro')['imagen'];

#3

Thanks!

I´m trying to acces to the originalName attribute, but cannot access it. I´m trying with this:

$request->files->get('registro')['imagen']['originalName'];


#4

You should read the Symfony docs on how to get the original name from an UploadedFile object.