2

I am working on a project using symfony.I want to use Sonata Media bundle,in order to upload image. Unfortunately I don't knwo how to use it or how to start.

I havethis form :

<form action="" method="POST" class="filePhotoForm form-inline" role="form">
                            <div class="form-group">
                                <input type="button" class="btn btn-start-order browse" value="Browse">
                                <input type="text" class="form-control file-name" readonly="readonly" placeholder="No file selected">
                                <input type="file" name="filePhoto" id="filePhoto" class="hidden file-upload">
                            </div><br/><br/>
                            <button type="submit" class="btn btn-primary">Submit</button>
                        </form>

And the code in controller:

 public function changePictureAction(Request $request)
    {


        return $this->render('MedAppBundle:Profile:change_picture.html.twig');
    }

Can you help with the basics of uploading?

Thank you!

Carmen Mitru
  • 151
  • 2
  • 12
  • Possible duplicate of [SonataMediaBundle - how to upload images?](http://stackoverflow.com/questions/11526457/sonatamediabundle-how-to-upload-images) – chalasr Feb 08 '16 at 15:53
  • Why do you need SonataMediaBundle ? If you just want upload images, look at the Symfony file upload recipes. http://symfony.com/doc/current/cookbook/doctrine/file_uploads.html http://symfony.com/doc/current/cookbook/controller/upload_file.html – chalasr Feb 08 '16 at 15:56

1 Answers1

0

i have made a custom function for that

protected function saveImageMediaBundle($file,$context = 'default')
{

    $mediaManager = $this->get('sonata.media.manager.media');
    $media = new Media();
    $media->setContext($context);
    $media->setProviderName('sonata.media.provider.image');
    $media->setBinaryContent($file);
    $mediaManager->save($media);

    return $media;
}

and get file by this

$file = $request->files->get('newsImage',null);
$media = $this->saveImageMediaBundle($file,'news');
M Maavia
  • 340
  • 1
  • 9