IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Symfony PHP Discussion :

image bouton upload dans un article [2.x]


Sujet :

Symfony PHP

  1. #1
    Membre éprouvé
    Homme Profil pro
    Développeur Web
    Inscrit en
    Novembre 2013
    Messages
    739
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Développeur Web

    Informations forums :
    Inscription : Novembre 2013
    Messages : 739
    Points : 1 022
    Points
    1 022
    Par défaut image bouton upload dans un article
    Bonjour,
    j'essaie d'integrer le bouton parcourir pour inserer a partir de ma machine une image dans un article .
    au debut lorsque j'ai declaré image de type string et j'ai inseré un lien http:// .... ça marche mais avec un bouton parcourir ça bloque !!

    ArticleControlleur :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    <?php
     
    namespace MyApp\EspritBundle\Controller;
     
    use Symfony\Bundle\FrameworkBundle\Controller\Controller;
    use Symfony\Component\HttpFoundation\Request;
    use MyApp\EspritBundle\Entity\Article;
    use MyApp\EspritBundle\Form\ArticleType;
     
    class ArticleController extends Controller {
     
     
     
         public function newAction()
        {
            $article = new Article();
            //$entity = $em->getRepository('CliniqueGynecoBundle:Article');
            $form   = $this->createForm(new ArticleType(), $article);
     
            return $this->render('MyAppEspritBundle:Article:send.html.twig', array(
                'article' => $article,
                'form'   => $form->createView()
            ));
        }
     
         public function sendAction() {
     
            $article = new Article();
            $form = $this->createForm(new ArticleType, $article);
            $request = $this->getRequest();
            if ($request->isMethod('Post')) {
     
                $form->bind($request);
     
                if ($form->isValid()) {
                    $article = $form->getData();
                    $em = $this->getDoctrine()->getManager();
                    $em->persist($article);
                    $em->flush();
     
                    return $this->redirect($this->generateUrl('my_app_esprit_article_show'));
                }
     
            }
     
            return $this->render('MyAppEspritBundle:Article:send.html.twig', array('form' => $form->createView()));
        }
     
     
     
        /**
         * Show a Article entry
         */
        public function showAction() {
            $em = $this->getDoctrine()->getManager();
     
            $article = $em->getRepository('MyAppEspritBundle:Article')->findBy(array(), array('date'=>'asc'));
     
            return $this->render('MyAppEspritBundle:Article:show.html.twig', array(
                        'article' => $article,
            ));
        }
     
     
        public function deleteAction($id) {
            $em = $this->getDoctrine()->getManager();
            $article = $em->getRepository('MyAppEspritBundle:Article')->find($id);
     
            if (!$article) {
                throw $this->createNotFoundException('No Article found for id ' . $id);
            }
     
            /*  On teste que l'utilisateur dispose bien du rôle ROLE_ADMIN*/      
              if (!$this->get('security.context')->isGranted('ROLE_ADMIN')) {
              return $this->redirect($this->generateUrl('fos_user_security_login'));
              }
            /**************** */
     
     
            $em->remove($article);
            $em->flush();
                             /********** FlashBag ****** */
             $this->get('session')->getFlashBag()->set('message', 'Cet article disparait !!'); 
                            /**************** */
            return $this->redirect($this->generateUrl('my_app_esprit_article_show'));
        }
     
        public function editAction($id, Request $request) {
     
            $em = $this->getDoctrine()->getManager();
            $article = $em->getRepository('MyAppEspritBundle:Article')->find($id);
            if (!$article) {
                throw $this->createNotFoundException(
                        'No Article found for id ' . $id
                );
            }
     
            $form = $this->createFormBuilder($article)
                    ->add('texte', 'text')
                    ->add('image', 'text', array('required' => false))
                    ->add('nom', 'text')
                    ->add('date', 'date')
                    ->add('role', 'entity', array(
                        'class' => 'MyApp\EspritBundle\Entity\Role',
                        'property' => 'permission',
                        'expanded' => false,
                        'multiple' => false,
                        'required' => true))
                    ->add('sousrubrique', 'entity', array(
                        'class' => 'MyApp\EspritBundle\Entity\Sousrubrique',
                        'property' => 'title',
                        'expanded' => false,
                        'multiple' => false,
                        'required' => false))
                    ->add('rubrique', 'entity', array(
                        'class' => 'MyApp\EspritBundle\Entity\Rubrique',
                        'property' => 'title',
                        'expanded' => false,
                        'multiple' => false,
                        'required' => false))
                    ->getForm();
     
            $form->handleRequest($request);
     
            if ($form->isValid()) {
                $em->flush();
                return $this->redirect($this->generateUrl('my_app_esprit_article_show'));
            }
     
            return $this->render('MyAppEspritBundle:Article:edit.html.twig', array('form' => $form->createView()));
        }
     
        public function topAction($max = 50) {
            $em = $this->container->get('doctrine')->getManager();
     
     
            /* recuperer article */
            $qb1 = $em->createQueryBuilder();
            $qb1->select('a')
                    ->from('MyAppEspritBundle:Article', 'a')
                    ->orderBy('a.date')
                    ->setMaxResults($max);
            $query1 = $qb1->getQuery();
            $article = $query1->getResult();
     
            /* recuperer rubrique */
            $qb2 = $em->createQueryBuilder();
            $qb2->select('b')
                    ->from('MyAppEspritBundle:Rubrique', 'b')
                    ->orderBy('b.position')
                    ->setMaxResults($max);
            $query2 = $qb2->getQuery();
            $rubrique = $query2->getResult();
     
            /* recuperer sousrubrique */
            $qb3 = $em->createQueryBuilder();
            $qb3->select('c')
                    ->from('MyAppEspritBundle:Sousrubrique', 'c')
                    ->orderBy('c.position')
                    ->setMaxResults($max);
            $query3 = $qb3->getQuery();
            $sousrubrique = $query3->getResult();
     
            /* recuperer actualite */
            $qb4 = $em->createQueryBuilder();
            $qb4->select('c')
                    ->from('MyAppEspritBundle:Actualite', 'c')
                    ->orderBy('c.dateinsertion')
                    ->setMaxResults($max);
            $query4 = $qb4->getQuery();
            $actualite = $query4->getResult();
     
            /* recuperer menu */
            $qb5 = $em->createQueryBuilder();
            $qb5->select('c')
                    ->from('MyAppEspritBundle:Menu', 'c')
                    ->orderBy('c.position')
                    ->setMaxResults($max);
            $query5 = $qb5->getQuery();
            $menu = $query5->getResult();
     
     
            /* recuperer role */
            $qb6 = $em->createQueryBuilder();
            $qb6->select('c')
                    ->from('MyAppEspritBundle:Role', 'c')
                    ->orderBy('c.permission')
                    ->setMaxResults($max);
            $query6 = $qb6->getQuery();
            $role = $query6->getResult();
     
     
     
     
     
     
     
            return $this->container->get('templating')
                            ->renderResponse('MyAppEspritBundle:Article:lister.html.twig', array(
                                'article' => $article,
                                'rubrique' => $rubrique,
                                'sousrubrique' => $sousrubrique,
                                'actualite' => $actualite,
                                'menu' => $menu,
                                'role' => $role,
            ));
        }
     
        public function showbyidAction($id) {
            $em = $this->getDoctrine()->getManager();
     
            $a = $em->getRepository('MyAppEspritBundle:Article')->find($id);
            /*         * ************** */
            /* recuperer article */
            $qb1 = $em->createQueryBuilder();
            $qb1->select('a')
                    ->from('MyAppEspritBundle:Article', 'a')
                    ->orderBy('a.date');
            $query1 = $qb1->getQuery();
            $article = $query1->getResult();
            /*         * ************************ */
            /* recuperer rubrique */
            $qb2 = $em->createQueryBuilder();
            $qb2->select('b')
                    ->from('MyAppEspritBundle:Rubrique', 'b')
                    ->orderBy('b.position');
            $query2 = $qb2->getQuery();
            $rubrique = $query2->getResult();
     
            /* recuperer sousrubrique */
            $qb3 = $em->createQueryBuilder();
            $qb3->select('c')
                    ->from('MyAppEspritBundle:Sousrubrique', 'c')
                    ->orderBy('c.position');
            $query3 = $qb3->getQuery();
            $sousrubrique = $query3->getResult();
     
            /* recuperer actualite */
            $qb4 = $em->createQueryBuilder();
            $qb4->select('c')
                    ->from('MyAppEspritBundle:Actualite', 'c')
                    ->orderBy('c.dateinsertion');
            $query4 = $qb4->getQuery();
            $actualite = $query4->getResult();
     
            /* recuperer menu */
            $qb5 = $em->createQueryBuilder();
            $qb5->select('c')
                    ->from('MyAppEspritBundle:Menu', 'c')
                    ->orderBy('c.position');
            $query5 = $qb5->getQuery();
            $menu = $query5->getResult();
     
     
            /* recuperer role */
            $qb6 = $em->createQueryBuilder();
            $qb6->select('c')
                    ->from('MyAppEspritBundle:Role', 'c')
                    ->orderBy('c.permission');
            $query6 = $qb6->getQuery();
            $role = $query6->getResult();
     
            /*         * ********* */
     
            if (!$article) {
                throw $this->createNotFoundException('Unable to find article entity.');
            }
     
            //$deleteForm = $this->createDeleteForm($id);
     
            return $this->render('MyAppEspritBundle:Article:showbyid.html.twig', array(
                        'article' => $article,    
                        'a' => $a,
                        'rubrique' => $rubrique,
                        'sousrubrique' => $sousrubrique,
                        'actualite' => $actualite,
                        'menu' => $menu,
                        'role' => $role,
            ));
        }
     
    }
    class Article:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    <?php
     
    namespace MyApp\EspritBundle\Entity;
     
    use Doctrine\ORM\Mapping as ORM;
    use Symfony\Component\Validator\Constraints as Assert;
     
    /**
     * Article
     * @ORM\HasLifecycleCallbacks
     * @ORM\Table()
     * @ORM\Entity
     */
    class Article {
     
        /**
         * @var integer
         * @ORM\Column(name="id", type="integer")
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        private $id;
     
        /**
         * @ORM\ManyToOne(targetEntity="MyApp\EspritBundle\Entity\Rubrique")
         * @ORM\JoinColumn(nullable=true, onDelete="CASCADE")
         */
        protected $rubrique;
     
        /**
         * @ORM\OneToOne(targetEntity="MyApp\EspritBundle\Entity\Sousrubrique", cascade={"remove", "persist"} )
         * @ORM\JoinColumn(name="sousrubrique_id", referencedColumnName="id", onDelete="CASCADE")
         */
        protected $sousrubrique;
     
        /**
         * @ORM\ManyToOne(targetEntity="MyApp\EspritBundle\Entity\Role", inversedBy="articles")
         * @ORM\JoinColumn(name="role_id", referencedColumnName="id",nullable=false, onDelete="CASCADE")
         */
        protected $role;
     
        /**
         * @var string
         * @Assert\Length(
         *      min = 2,
         *      max = 50,
         *      minMessage = "Your first name must be at least {{ limit }} characters long",
         *      maxMessage = "Your first name cannot be longer than {{ limit }} characters long"
         * )   
         * @ORM\Column(name="texte", type="string", length=500 )
         */
        private $texte;
     
        /**
         * @var string $image
         * @Assert\File( maxSize = "1024k", mimeTypesMessage = "Please upload a valid Image")
         * @ORM\Column(name="image", type="string", length=255)
         */
        private $image;
     
        /**
         * @var string
         * @Assert\Length(min = 5, max = 50)  
         * @ORM\Column(name="nom", type="string", length=255,unique=true)
         */
        private $nom;
     
        /**
         * @var \DateTime
         *
         * @ORM\Column(name="date", type="date")
         */
        private $date;
     
        public function __construct() {
            $this->date = new \Datetime();
        }
     
        /**
         * Get id
         *
         * @return integer 
         */
        public function getId() {
            return $this->id;
        }
     
        /**
         * Set texte
         *
         * @param string $texte
         * @return Article
         */
        public function setTexte($texte) {
            $this->texte = $texte;
     
            return $this;
        }
     
        /**
         * Get texte
         *
         * @return string 
         */
        public function getTexte() {
            return $this->texte;
        }
     
        /**
         * Set image
         *
         * @param string $image
         * @return Article
         */
        public function setImage($image) {
            $this->image = $image;
     
            return $this;
        }
     
        /**
         * Get image
         *
         * @return string 
         */
        public function getImage() {
            return $this->image;
        }
     
        /**
         * Set nom
         *
         * @param string $nom
         * @return Article
         */
        public function setNom($nom) {
            $this->nom = $nom;
     
            return $this;
        }
     
        /**
         * Get nom
         *
         * @return string 
         */
        public function getNom() {
            return $this->nom;
        }
     
        /**
         * Set date
         *
         * @param \DateTime $date
         * @return Article
         */
        public function setDate($date) {
            $this->date = $date;
     
            return $this;
        }
     
        /**
         * Get date
         *
         * @return \DateTime 
         */
        public function getDate() {
            return $this->date;
        }
     
        public function getRubrique() {
            return $this->rubrique;
        }
     
        public function getSousrubrique() {
            return $this->sousrubrique;
        }
     
        public function setRubrique($rubrique) {
            $this->rubrique = $rubrique;
        }
     
        public function setSousrubrique($sousrubrique) {
            $this->sousrubrique = $sousrubrique;
        }
     
        public function getRole() {
            return $this->role;
        }
     
        public function setRole($role) {
            $this->role = $role;
        }
     
        public function __toString() {
            return $this->title . '' . $this->response;
        }
     
        public function getFullImagePath() {
            return null === $this->image ? null : $this->getUploadRootDir() . $this->image;
        }
     
        protected function getUploadRootDir() {
            // the absolute directory path where uploaded documents should be saved
            return $this->getTmpUploadRootDir() . $this->getId() . "/";
        }
     
        protected function getTmpUploadRootDir() {
            // the absolute directory path where uploaded documents should be saved
            return __DIR__ . '/../../../../web/upload/';
        }
     
        /**
         * @ORM\PrePersist()
         * @ORM\PreUpdate()
         */
        public function uploadImage() {
            // the file property can be empty if the field is not required
            if (null === $this->image) {
                return;
            }
            if (!$this->id) {
                $this->image->move($this->getTmpUploadRootDir(), $this->image->getClientOriginalName());
            } else {
                $this->image->move($this->getUploadRootDir(), $this->image->getClientOriginalName());
            }
            $this->setImage($this->image->getClientOriginalName());
        }
     
        /**
         * @ORM\PostPersist()
         */
        public function moveImage() {
            if (null === $this->image) {
                return;
            }
            if (!is_dir($this->getUploadRootDir())) {
                mkdir($this->getUploadRootDir());
            }
            copy($this->getTmpUploadRootDir() . $this->image, $this->getFullImagePath());
            unlink($this->getTmpUploadRootDir() . $this->image);
        }
     
        /**
         * @ORM\PreRemove()
         */
        public function removeImage() {
            unlink($this->getFullImagePath());
            rmdir($this->getUploadRootDir());
        }
     
     
    }
    ArticeType:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    <?php
     
    namespace MyApp\EspritBundle\Form;
     
    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\FormBuilderInterface;
    use Symfony\Component\OptionsResolver\OptionsResolverInterface;
     
    class ArticleType extends AbstractType {
     
        /**
         * @param FormBuilderInterface $builder
         * @param array $options
         */
        public function buildForm(FormBuilderInterface $builder, array $options) {
            $builder
            ->add('texte','textarea')
            ->add('image')
            ->add('nom')
            ->add('date')
     
     
     
            ->add('role', 'entity', 
                    array(
              'class' => 'MyApp\EspritBundle\Entity\Role',   
              'property' => 'response',
              'expanded' =>false,
              'multiple' => false,
              'required' => true ) )    
     
     
     
            ->add('sousrubrique', 'entity', 
                    array(
              'class' => 'MyApp\EspritBundle\Entity\Sousrubrique',   
              'property' => 'title',
              'expanded' => false,
              'multiple' => false,
              'required' => false ) )   
     
     
     
            ->add('rubrique', 'entity', 
                    array(
              'class' => 'MyApp\EspritBundle\Entity\Rubrique',     
              'property' => 'title',
              'expanded' => false,
              'multiple' => false,
              'required' => false ) 
     
     
     
     
           );
        }
     
        /**
         * @param OptionsResolverInterface $resolver
         */
        public function setDefaultOptions(OptionsResolverInterface $resolver) {
            $resolver->setDefaults(array(
                'data_class' => 'MyApp\EspritBundle\Entity\Article'
            ));
        }
     
        /**
         * @return string
         */
        public function getName() {
            return 'myapp_espritbundle_article';
        }
     
    }
    pas de message d'erreur :/

    merci d'avance .

  2. #2
    Membre habitué
    Ingénieur d'études et de développement
    Inscrit en
    Juin 2009
    Messages
    112
    Détails du profil
    Informations professionnelles :
    Activité : Ingénieur d'études et de développement
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Juin 2009
    Messages : 112
    Points : 154
    Points
    154

  3. #3
    Membre éprouvé
    Homme Profil pro
    Développeur Web
    Inscrit en
    Novembre 2013
    Messages
    739
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Développeur Web

    Informations forums :
    Inscription : Novembre 2013
    Messages : 739
    Points : 1 022
    Points
    1 022
    Par défaut
    j'ai corrigé la form twig.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    <form action="{{ path('basearticle_create') }}" method="post" {{ form_enctype(form) }}>

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. ExtJs 3.3 Ajout d'un bouton upload image dans un html editor
    Par khaled_s dans le forum Ext JS / Sencha
    Réponses: 3
    Dernier message: 17/02/2012, 10h15
  2. [Tableaux] Uploade images et enregistrement dans BD
    Par dunbar dans le forum Langage
    Réponses: 9
    Dernier message: 27/11/2006, 14h53
  3. Uploade images et enregistrement dans BD
    Par dunbar dans le forum Outils
    Réponses: 2
    Dernier message: 20/11/2006, 22h17

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo