コード例 #1
0
 public function add(Objet $obj, $file)
 {
     // calcul d'un nouveau code d'itineraire non déja utilisé = Maximum + 1
     $obj->setIdObj($this->db->query("SELECT MAX(IDOBJ) AS MAXIMUM FROM objets")->fetchColumn() + 1);
     //nouvel ID
     if (isset($file) and $file['error'] == 0) {
         if ($file['size'] <= 1000000000) {
             $infosfichier = pathinfo($file['name']);
             $extension_upload = $infosfichier['extension'];
             $extensions_autorisees = ['jpg', 'png', 'gif'];
             if (in_array($extension_upload, $extensions_autorisees)) {
                 move_uploaded_file($file['tmp_name'], './images/' . $obj->getIdObj() . '.' . $extension_upload);
             }
         }
     }
     $photo = $obj->getIdObj() . '.' . $extension_upload;
     // requete d'ajout dans la BD
     $req = "INSERT INTO objets (idobj,idcategorie,iduser,nom,prixini,datedebut,datefin,description,photo) VALUES(:idobj, :idcategorie, :iduser, :nom, :prixini, NOW(), :datefin, :description, :photo)";
     $add = $this->db->prepare($req);
     $ok = $add->execute(array("idobj" => $obj->getIdObj(), "idcategorie" => $obj->getIdCategorie(), "iduser" => $obj->getIdUser(), "nom" => $obj->getNom(), "prixini" => $obj->getPrixIni(), "datefin" => $obj->getDateFin(), "description" => $obj->getDescription(), "photo" => $photo));
     return $ok;
 }