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

Zend Framework PHP Discussion :

ZF2 FormCollection multiple file upload not upload in the destination but not errors


Sujet :

Zend Framework PHP

  1. #1
    Membre régulier
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Décembre 2004
    Messages
    319
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 41
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Distribution

    Informations forums :
    Inscription : Décembre 2004
    Messages : 319
    Points : 83
    Points
    83
    Par défaut ZF2 FormCollection multiple file upload not upload in the destination but not errors
    Hello,

    I want to do a page with form who contain multiple file. I use ZF2 and Collection with the possibility to add a new and remove....
    When submit it not take erros and not save the image to the repertory ....

    My Form :
    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
     
        <?php
        namespace User\Form;
     
        use Zend\InputFilter;
     
        use Zend\Captcha;
        use Zend\Form\Element;
        use Zend\Form\Form;
     
        class DiaporamaForm extends Form
        {
            private $_numFileElements = 1;
     
            public function _($str)
            {
                return $str;
            }
     
            public function getMaxImages()
            {
                return $this->_numFileElements;
            }
     
            public function __construct($nbImagesMax = 1)
            {
                parent::__construct('diaporama');
     
                $this->_numFileElements = $nbImagesMax;
     
     
                $this->setAttribute('method', 'post');
                $this->setAttribute('enctype', 'multipart/form-data');
                $this->setAttribute('novalidate', 'novalidate');
     
                $this->setAttribute('autocomplete', 'off');
     
                $this->addElements();
                $this->setInputFilter($this->createInputFilter());
            }
     
            public function addElements()
            {
                $this->add(array(
                    'name' => 'id',
                    'type' => 'Zend\Form\Element\Hidden',
                    'attributes' => array(),
                    'options' => array(
                        'label' => $this->_('Identifiant of the diaporama'),
                        //'column-size' => 'sm-10',
                        //'label_attributes' => array('class' => 'col-sm-2')
                    ),
                ));
     
                $this->add(array(
                    'name' => 'idUser',
                    'type' => 'Zend\Form\Element\Hidden',
                    'attributes' => array(
                        'required' => 'required',
                    ),
                    'options' => array(
                        'label' => $this->_('Identification of user'),
                        //'column-size' => 'sm-10',
                        //'label_attributes' => array('class' => 'col-sm-2')
                    ),
                ));
     
                $this->add(array(
                    'name' => 'titreDiapo',
                    'type' => 'Zend\Form\Element\Text',
                    'attributes' => array(
                        'required' => 'required',
                    ),
                    'options' => array(
                        'label' => $this->_('Name of the diaporama'),
                        //'column-size' => 'sm-10',
                        //'label_attributes' => array('class' => 'col-sm-2')
                    ),
                ));
     
                $this->add(array(
                    'name' => 'role',
                    'type' => 'Zend\Form\Element\Radio',
                    'attributes' => array(
                        'required' => 'required',
                        'value' => 'Public',
                    ),
                    'options' => array(
                        'label' => $this->_('Status'),
                        'value_options' => array(
                            'Private' => $this->_('Private'),
                            'Public' => $this->_('Public'),
                        ),
                        //'column-size' => 'sm-10',
                        //'label_attributes' => array('class' => 'col-sm-2')
                    ),
                ));
     
                // File Input
                $file = new Element\File('file');
                $file->setLabel($this->_('Titre of the imege'));
                $file->setAttribute("class", "fielsetFile upload");
                $file->setAttribute("required", false);
     
     
                $this->add(array(
                    'type' => 'Zend\Form\Element\Collection',
                    'name' => 'file-collection',
                    'attributes' => array(
                        'class' => "fielsetFile form-control"
                    ),
                    'options' => array(
                        'label' => $this->_('New image'),
                        'help-block' => $this->_("We accept only the following formats: 'jpg', 'jpeg', 'png' and 'gif'"),
                        'count' => $this->_numFileElements,
                        'should_create_template' => true,
                        'allow_add' => true,
                        'allow_remove' => true,
                        'template_placeholder' => '__file-collection__',
                        'target_element' => $file
                    ),
                ));
     
                for( $i=0; $i < $this->_numFileElements; $i++ ) {
                    $this->add(array(
                        'name' => 'titre_'.$i,
                        'type' => 'Zend\Form\Element\Text',
                        'attributes' => array(
                            'id' => "titre_".$i,
                            'class' => "fielsetFile form-control"
                        ),
                        'options' => array(
                            'label' => $this->_('Titre of the imege'),
                            //'column-size' => 'sm-10',
                            //'label_attributes' => array('class' => 'col-sm-2')
                        ),
                    ));
                    $this->add(array(
                        'name' => 'commentaire_'.$i,
                        'type' => 'Zend\Form\Element\Textarea',
                        'attributes' => array(
                            'id' => 'commentaire_'.$i,
                            'class' => "fielsetFile editor form-control"
                        ),
                        'options' => array(
                            'label' => $this->_('Comment'),
                            //'column-size' => 'sm-10',
                            //'label_attributes' => array('class' => 'col-sm-2')
                        ),
                    ));
                )
     
                $this->add(array(
                    'name' => 'back',
                    'type' => 'button',
                    'attributes' => array('type' => 'submit'),
                    'options' => array(
                        'label' => $this->_('Back'),
                        'column-size' => 'sm-10 col-sm-offset-2'
                    )
                ));
     
                $this->add(array(
                    'name' => 'submit',
                    'type' => 'button',
                    'attributes' => array('type' => 'submit', 'class' => 'btn-primary'),
                    'options' => array(
                        'label' => $this->_('Validate'),
                        'column-size' => 'sm-10 col-sm-offset-2'
                    )
                ));
            }
     
            public function createInputFilter()
            {
                $inputFilter = new InputFilter\InputFilter();
     
                // ici le nombre de fichiers à uploader
                // 
                // File Collection
                $fileCollection = new InputFilter\InputFilter();
                for ($i = 0; $i < $this->_numFileElements; $i++) {
                    $file = new InputFilter\FileInput($i);
                    //$file->setRequired(false);
                    $file->setRequired(true);
                    $file->getFilterChain()->attachByName(
                        'filerenameupload',
                        array(
                            'target' => './data/diaporamas/',
                            'overwrite' => true,
                            'use_upload_name' => true,
                            'randomize'       => true
                        )
                    );
                    $fileCollection->add($file);
                }
                $inputFilter->add($fileCollection, 'file-collection');
     
     
                // Text Input
                $idUser = new InputFilter\Input('idUser');
                $idUser->setRequired(true);
                $inputFilter->add($idUser);
     
                $titreDiapo = new InputFilter\Input('titreDiapo');
                $titreDiapo->setRequired(true);
                $inputFilter->add($titreDiapo);
     
                return $inputFilter;
            }
     
        }
    The Input filter seems indicate the destination but why not upload ????

    I do the same that the exemple :https://github.com/cgmartin/ZF2FileUploadExamples

    Thanks for help

  2. #2
    Membre régulier
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Décembre 2004
    Messages
    319
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 41
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Distribution

    Informations forums :
    Inscription : Décembre 2004
    Messages : 319
    Points : 83
    Points
    83
    Par défaut
    Bonjour,

    Finallement,

    J'ai vu qu'il fallait passer par des FormCollection ? Je me trompe donc j'ai fait comme cela :
    Formulaire :
    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
    <?php
    namespace User\Form;
     
    use Zend\InputFilter;
     
    use Zend\Captcha;
    use Zend\Form\Element;
    use Zend\Form\Form;
     
    class DiaporamaForm extends Form
    {
        private $_numFileElements = 1;
     
        public function _($str)
        {
            return $str;
        }
     
        public function getMaxImages()
        {
            return $this->_numFileElements;
        }
     
        public function __construct($nbImagesMax = 1)
        {
            parent::__construct('diaporama');
     
            $this->_numFileElements = $nbImagesMax;
     
     
            $this->setAttribute('method', 'post');
            $this->setAttribute('enctype', 'multipart/form-data');
            $this->setAttribute('novalidate', 'novalidate');
     
            $this->setAttribute('autocomplete', 'off');
     
            $this->addElements();
            $this->setInputFilter($this->createInputFilter());
        }
     
        public function addElements()
        {
            $this->add(array(
                'name' => 'id',
                'type' => 'Zend\Form\Element\Hidden',
                'attributes' => array(),
                'options' => array(
                    'label' => $this->_('Identifiant of the diaporama'),
                    //'column-size' => 'sm-10',
                    //'label_attributes' => array('class' => 'col-sm-2')
                ),
            ));
     
            $this->add(array(
                'name' => 'idUser',
                'type' => 'Zend\Form\Element\Hidden',
                'attributes' => array(
                    'required' => 'required',
                ),
                'options' => array(
                    'label' => $this->_('Identification of user'),
                    //'column-size' => 'sm-10',
                    //'label_attributes' => array('class' => 'col-sm-2')
                ),
            ));
     
            $this->add(array(
                'name' => 'titreDiapo',
                'type' => 'Zend\Form\Element\Text',
                'attributes' => array(
                    'required' => 'required',
                ),
                'options' => array(
                    'label' => $this->_('Name of the diaporama'),
                    //'column-size' => 'sm-10',
                    //'label_attributes' => array('class' => 'col-sm-2')
                ),
            ));
     
            $this->add(array(
                'name' => 'role',
                'type' => 'Zend\Form\Element\Radio',
                'attributes' => array(
                    'required' => 'required',
                    'value' => 'Public',
                ),
                'options' => array(
                    'label' => $this->_('Status'),
                    'value_options' => array(
                        'Private' => $this->_('Private'),
                        'Public' => $this->_('Public'),
                    ),
                    //'column-size' => 'sm-10',
                    //'label_attributes' => array('class' => 'col-sm-2')
                ),
            ));
     
            $this->add(array(
                'type' => 'Zend\Form\Element\Collection',
                'name' => 'file-collection',
                'attributes' => array(
                    'class' => "fielsetFile form-control"
                ),
                'options' => array(
                    'label' => $this->_('New image'),
                    'help-block' => $this->_("We accept only the following formats: 'jpg', 'jpeg', 'png' and 'gif'"),
                    'count' => $this->_numFileElements,
                    'should_create_template' => true,
                    'allow_add' => true,
                    'allow_remove' => true,
                    'template_placeholder' => '__file-collection__',
                    'target_element' => new CustomFileFieldset('file') //$file
                ),
            ));
     
            $this->add(array(
                'name' => 'back',
                'type' => 'button',
                'attributes' => array('type' => 'submit'),
                'options' => array(
                    'label' => $this->_('Back'),
                    'column-size' => 'md-10 col-md-offset-2'
                )
            ));
     
            $this->add(array(
                'name' => 'submit',
                'type' => 'button',
                'attributes' => array('type' => 'submit', 'class' => 'btn-primary'),
                'options' => array(
                    'label' => $this->_('Validate'),
                    'column-size' => 'md-10 col-md-offset-2'
                )
            ));
        }
     
        public function createInputFilter()
        {
            $inputFilter = new InputFilter\InputFilter();
     
            // ici le nombre de fichiers à uploader
            //
            // File Collection
            $fileCollection = new InputFilter\InputFilter();
            for ($i = 0; $i < $this->_numFileElements; $i++) {
                $file = new InputFilter\FileInput($i);
                $file->setRequired(true);
                $file->getFilterChain()->attachByName(
                    'filerenameupload',
                    array(
                        'target' => realpath('./data/diaporamas/'),
                        'overwrite' => true,
                        'use_upload_name' => true,
                        //'randomize'       => true,
                        //'use_upload_name' => false
                    )
                );
                $fileCollection->add($file);
            }
            $inputFilter->add($fileCollection, 'file-collection');
     
     
            // Text Input
            $idUser = new InputFilter\Input('idUser');
            $idUser->setRequired(true);
            $inputFilter->add($idUser);
     
            $titreDiapo = new InputFilter\Input('titreDiapo');
            $titreDiapo->setRequired(true);
            $inputFilter->add($titreDiapo);
     
     
            return $inputFilter;
        }
     
    }
    Le CustomFileFieldset :
    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
    <?php
    namespace User\Form;
     
    use User\Model\Entity\Diaporama;
    use Zend\Form\Fieldset;
    use Zend\Form\Element;
    use Zend\InputFilter\InputFilterProviderInterface;
    use Zend\Stdlib\Hydrator\ClassMethods as ClassMethodsHydrator;
     
    class CustomFileFieldset extends Fieldset implements InputFilterProviderInterface
    {
        public function _($str)
        {
            return $str;
        }
     
        public function __construct($name = 'file')
        {
            parent::__construct($name);
     
     
            //$this->setHydrator(new ClassMethodsHydrator())->setObject(new Diaporama());
     
            $this->add(array(
                'name' => 'upload',
                'type' => 'Zend\Form\Element\File',
                'attributes' => array(
                    'class' => "fielsetFile upload",
                    //'required' => 'required',
                    'multiple' => false, // html 5
                ),
                'options' => array(
                    'label' => $this->_('New image'),
                    'help-block' => $this->_("We accept only the following formats: 'jpg', 'jpeg', 'png' and 'gif'")
                )
            ));
     
            $this->add(array(
                'name' => 'titre',
                'type' => 'Zend\Form\Element\Text',
                'attributes' => array(
                    'class' => "fielsetFile form-control"
                ),
                'options' => array(
                    'label' => $this->_('Titre of the imege'),
                    //'column-size' => 'sm-10',
                    //'label_attributes' => array('class' => 'col-sm-2')
                ),
            ));
     
            $this->add(array(
                'name' => 'commentaire',
                'type' => 'Zend\Form\Element\Textarea',
                'attributes' => array(
                    'class' => "fielsetFile editor form-control"
                ),
                'options' => array(
                    'label' => $this->_('Comment'),
                    //'column-size' => 'sm-10',
                    //'label_attributes' => array('class' => 'col-sm-2')
                ),
            ));
        }
        public function getInputFilterSpecification()
        {
            return array(
                'upload' => array(
                    'required' => true,
                ),
                'titre' => array(
                    'required' => true,
                ),
                'commentaire' => array(
                    'required' => true,
                ),
            );
        }
     
    }
    Mon action :
    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
    if ($this->isConnected() === null) {
                $viewModel = new ViewModel();
                $viewModel->setTemplate("user/NotConnected");
     
                return $viewModel;
            } else {
                $otherErrors = array();
     
                try {
                    $idUser = $this->getIdUser();
     
                    //var_dump($idUser);die;
     
                    $abonnement = $this->getAbonnementsTable();
                    $aboUser = $abonnement->getAbonnementByIdUser($idUser)->current();
                    $nbImagesParDiaporama = $aboUser['nbImagesParDiaporama'];
     
                    $form = new DiaporamaForm();
     
                    $dataForm = array(
                        "idUser" => $idUser
                    );
     
                    $form->setData($dataForm);
     
                    if ($this->getRequest()->isPost()) {
                        // Postback
                        $data = array_merge_recursive(
                            $this->getRequest()->getPost()->toArray(),
                            $this->getRequest()->getFiles()->toArray()
                        );
     
                        //var_dump($data);die;
     
                        $form->setData($data);
     
     
                        $diaporama = new Diaporama();
                        $diaporama->exchangeArray($data);
     
     
                        if ($form->isValid()) {
                            //
                            // ...Save the form...
                            //
                            //
                            var_dump($data);
                            die;
     
                            $diapoTable = $this->getDiaporamaTable();
     
                            $response = $diapoTable->saveDiaporama($diaporama);
     
                            if ($response) {
                                $idDiaporamaNew = $response;
     
                                return $this->redirect()->toRoute('book');
                            }
                            //return $this->redirectToSuccessPage($form->getData());
                        }
                        else {
                            $otherErrors[] = "The images could not be transmitted.";
                        }
                    }
                }
                catch(\Exception $e) {
                    $otherErrors[] = $e->getMessage();
                }
            }
     
            return array('form' => $form, 'otherErrors' => $otherErrors);

    Ce que je ne comprend pas pourquoi il me met l'erreur : "The images could not be transmitted." ?

    Ou es-ce que j'ai mal fait ?

    J'ai pourtant fait : dans le filter : 'target' => realpath('./data/diaporamas/'),

    Aussi quand je fais : var_dump($data);die;

    j'ai :
    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
    array (size=6)
      'id' => string '' (length=0)
      'idUser' => string '1' (length=1)
      'titreDiapo' => string 'cvbvcbcvbc' (length=10)
      'role' => string 'Public' (length=6)
      'file-collection' => 
        array (size=2)
          0 => 
            array (size=2)
              'titre' => string 'TITRE IMAGE' (length=11)
              'commentaire' => string 'COMMENTAIRE IMAGE<br/>' (length=22)
          1 => 
            array (size=1)
              'upload' => 
                array (size=5)
                  ...
      'submit' => string '' (length=0)
    Du coup quand je reviens dans le form il m'affiche 2 fichiers alors que j'ai validé qu'un pour ce test....

    Merci de votre aide.

Discussions similaires

  1. Réponses: 1
    Dernier message: 09/01/2015, 16h49
  2. [2.x] Multiple File Upload
    Par Rosees dans le forum Symfony
    Réponses: 16
    Dernier message: 16/01/2014, 23h46
  3. [Upload] outil d'upload multiple files
    Par mkaffel dans le forum Langage
    Réponses: 5
    Dernier message: 17/07/2011, 21h46
  4. [Multiple File Upload Plugin] Récupére l'extension
    Par Shandler dans le forum jQuery
    Réponses: 2
    Dernier message: 02/07/2009, 10h21
  5. [Upload] Upload multiple files
    Par aymanov dans le forum Langage
    Réponses: 17
    Dernier message: 22/12/2008, 09h38

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