Problème Upload de fichiers avec 2 champs pour 1 Modèle

Par KokAz, il y a 14 ans


Bonjour,

Voilà mon problème est le suivant. J'ai une table fichiers avec son modèle et un table agences avec son modèle et son controller. Ces deux modèles sont lié par une relation HABTM. Mon problème se situe au niveau de ma fonction add, en effet, j'ai mis dans ma vue 2 champs d'un même modèle :

<?php echo $form->file('Fichier.0'); ?>
<?php echo $form->file('Fichier.1'); ?>

Je voulais ajouté via une manipulation de $this->data un pdf et une image dans ma table agences_fichiers et dans la table fichiers les info des fichiers. Je m'y suis pris de la manière suivante :

// {APP}/controllers/agences_controller.php -> function add();

$this->data'Agence']'name'] = strtoupper($this->data'Agence']'name']);
$this->data'Agence']'address'] = nl2br($this->data'Agence']'address']);
if (!empty($this->data'Fichier'])) {
    if (!empty($this->data'Fichier'][0])) {
        $type = $this->data'Fichier'][0]'type'];
        $size = $this->data'Fichier'][0]'size'];
        if ($type == 'image/jpeg' || $type == 'image/png' || $type == 'image/gif') {
            move_uploaded_file($this->data'Fichier'][0]'tmp_name'], "upload/images/" . $name =
            $this->data'Agence']'name'] . '-' . substr(uniqid(rand()), 0, 5) . '.' . 
             substr($this->data'Fichier'][0]'name'], -3));
             $this->Image->resize('upload/images/' . $name, 'upload/images/thumb/'.$name, 256, 256);
             $this->data'Fichier'] = array(
                 '1' => $name,
                 '2' => $type,
                 '3' => $size,
              );
              $this->set('test', $this->data);
              $this->Fichier->create();
              $this->Fichier->save($this->data'Fichier']);
              $this->Agence->save($this->data);
              }
          }
          if (!empty($this->data'Fichier'][1])) {
              $type = $this->data'Fichier'][1]'type'];
              $size = $this->data'Fichier'][1]'size'];
              if ($type != 'image/jpeg' || $type != 'image/png' || $type != 'image/gif') {
                  move_uploaded_file($this->data'Fichier'][1]'tmp_name'], "upload/pdf/" . 
                  $name = $this->data'Agence']'name'] . '-' . substr(uniqid(rand()), 0, 5) . '.' .
                  substr($this->data'Fichier'][1]'name'], -3));
                  $this->data'Fichier'] = array(
                      'name' => $name,
                      'type' => $type,
                      'size' => $size,
                  );
                  $this->Fichier->create();
                  $this->Fichier->save($this->data'Fichier']);
                  $this->Agence->save($this->data);
                 }
               }
            }

// {APP}/models/agence.php
var $hasAndBelongsToMany = array(
        'Fichier' => array(
            'className' => 'Fichier',
            'joinTable' => 'agences_fichiers',
            'foreignKey' => 'agence_id',
            'associationForeignKey' => 'fichier_id',
            'unique' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'finderQuery' => '',
            'deleteQuery' => '',
            'insertQuery' => ''
        )
    );

Si vous pouvez m'aider ça serait sympa si vous avez d'autres infos à me demander n’hésitez pas
Merci d'avance,
KokAz

5 réponses

imars, il y a 14 ans

Finalement c'est quoi qui ne marche pas ?

KokAz, il y a 14 ans

En fait quand j'upload deux fichiers un PDF et une image ça ne m'enregistre que l'image dans ma table fichiers mais pas dans la table HABTM. Même si j'envoi q'un fichier ça fait la même chose. Je suis sur que c'est une erreur idiote :'(

djtec, il y a 14 ans

Normal qu'il enregistre pas ton pdf car tu regarde seulement si c'est une image gif, png, jpg en aucun cas tu upload si c'est du pdf

KokAz, il y a 14 ans

Si regarde à la ligne 28 :

if ($type != 'image/jpeg' || $type != 'image/png' || $type != 'image/gif')
djtec, il y a 14 ans

Oulah oui désolé j'ai mal vu ben c'est bizarre alors car je vois rien qui cloche