Created
June 21, 2014 20:20
-
-
Save cyb3rd4d/8f3db064ed5ebd75d19a to your computer and use it in GitHub Desktop.
An example of a file upload with Symfony2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Martial\UploadBundle\Controller; | |
use Martial\UploadBundle\Entity\Image; | |
use Martial\UploadBundle\Form\Type\ImageType; | |
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
class DefaultController extends Controller | |
{ | |
public function indexAction() | |
{ | |
$form = $this->createForm(new ImageType(), new Image()); | |
return $this->render('MartialUploadBundle:Default:index.html.twig', array('form' => $form->createView())); | |
} | |
public function handleAction() | |
{ | |
$request = $this->get('request'); | |
$image = new Image(); | |
$form = $this->createForm(new ImageType(), $image); | |
$form->handleRequest($request); | |
if ($form->isValid()) { | |
$file = $form['image']->getData(); | |
$extension = $file->guessExtension(); | |
$targetDirectory = '/target/path/'; | |
if (!$extension) { | |
$extension = 'bin'; | |
} | |
$file->move($targetDirectory, rand(1, 99999) . '.' . $extension); | |
} | |
return $this->redirect($this->generateUrl('martial_upload_homepage')); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Martial\UploadBundle\Entity; | |
class Image | |
{ | |
/** | |
* @var string | |
*/ | |
private $image; | |
/** | |
* @param string $image | |
*/ | |
public function setImage($image) | |
{ | |
$this->image = $image; | |
} | |
/** | |
* @return string | |
*/ | |
public function getImage() | |
{ | |
return $this->image; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Martial\UploadBundle\Form\Type; | |
use Symfony\Component\Form\AbstractType; | |
use Symfony\Component\Form\FormBuilderInterface; | |
use Symfony\Component\OptionsResolver\OptionsResolverInterface; | |
class ImageType extends AbstractType | |
{ | |
/** | |
* @param FormBuilderInterface $builder | |
* @param array $options | |
*/ | |
public function buildForm(FormBuilderInterface $builder, array $options = array()) | |
{ | |
$builder->add('image', 'file'); | |
} | |
/** | |
* @param OptionsResolverInterface $resolver | |
*/ | |
public function setDefaultOptions(OptionsResolverInterface $resolver) | |
{ | |
$resolver->setDefaults( | |
array('data_class' => '\Martial\UploadBundle\Entity\Image') | |
); | |
} | |
/** | |
* Returns the name of this type. | |
* | |
* @return string The name of this type | |
*/ | |
public function getName() | |
{ | |
return 'image_form'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% extends "::base.html.twig" %} | |
{% block body %} | |
{% for flashMessage in app.session.flashbag.get('notice') %} | |
<div class="flash-notice"> | |
{{ flashMessage }} | |
</div> | |
{% endfor %} | |
<h1>Upload image</h1> | |
<form action="{{ url("martial_upload_handle") }}" method="post" enctype="multipart/form-data"> | |
<p> | |
{{ form_label(form.image) }}<br /> | |
{{ form_widget(form.image) }} | |
</p> | |
<p> | |
{{ form_rest(form) }} | |
<input type="submit" value="Send" /> | |
</p> | |
</form> | |
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
martial_upload_homepage: | |
pattern: / | |
defaults: { _controller: MartialUploadBundle:Default:index } | |
martial_upload_handle: | |
pattern: /upload | |
defaults: { _controller: MartialUploadBundle:Default:handle } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Merci!!!!!