Ver Mensaje Individual
  #3 (permalink)  
Antiguo 03/12/2015, 18:23
Avatar de detective_jd
detective_jd
 
Fecha de Ingreso: abril-2011
Ubicación: Salto
Mensajes: 437
Antigüedad: 13 años, 9 meses
Puntos: 6
Respuesta: Subir Imágenes con php + MVC

Cita:
Iniciado por detective_jd Ver Mensaje
hola a todos, quería hacerles la sgte consulta, estoy usando MVC a mano y resulta que necesito trabajar con imagenes: en la página me da el sgte error:

Notice: Undefined index: foto in /var/www/html/amnesia_1/Controller/VehiculosController.php on line 44

el código en el VehiculoController es este:

Código PHP:
Ver original
  1. public function add(){
  2.         if(Session::get("tipo") == "admin"){
  3.             Session::set('mod', isset($_POST['txtmod']) ? $_POST['txtmod'] : Session::get('mod'));
  4.             $modelos = (Session::get('mod')!="") ? $this->mod_v->obtenerXDataList(Session::get('mod')) : array();
  5.             $tiposveh= $this->mod_tv->obtenerTodos();
  6.             if (isset($_POST['btnaceptar'])) {
  7.                 if(empty($_POST['txtmat']) or empty($_POST['txtcant']) or empty($_POST['txtmod'])){
  8.                     Session::set("msg","Ingrese los datos obligatorios (*) para continuar.");
  9.                     Session::set('modelos', $modelos);
  10.                     Session::set('tiposveh', $tiposveh);
  11.                     $this->redirect(array('add.php'));
  12.                 }
  13.                 else{
  14.                     if($this->checkImage($_FILES['foto'])!= null){
  15.                        
  16.                     }
  17.                     else {
  18.                         Session::set("msg","Hubo un fallo al subir la imagen");
  19.                         Session::set('modelos', $modelos);
  20.                         Session::set('tiposveh', $tiposveh);
  21.                         $this->redirect(array('add.php'));
  22.                     }
  23.                 }
  24.             }
  25.             else {
  26.                 Session::set('modelos', $modelos);
  27.                 Session::set('tiposveh', $tiposveh);
  28.                 $this->redirect(array('add.php'));
  29.             }
  30.         }
  31.         else {
  32.             Session::set("msg","Debe ser administrador para acceder.");
  33.             $this->redirect(array('Main','index.php'));
  34.         }
  35.     }
  36.    
  37.     private function checkImage($file){
  38.         if ($file["error"] > 0){
  39.            // echo "ha ocurrido un error";
  40.            return null;
  41.         } else {
  42.             $permitidos = array("image/jpg", "image/jpeg", "image/gif", "image/png");
  43.             $nombre = $file['name'];
  44.             $limite_kb = 100;
  45.             if (in_array($file['type'], $permitidos) && $file['size'] <= $limite_kb * 1024){
  46.         $ruta = "View/Layout/upload" . $nombre;
  47.         if (!file_exists($ruta)){
  48.                     $resultado = @move_uploaded_file($file["tmp_name"], $ruta);
  49.                     return ($resultado) ? $nombre : null;
  50.         } /*else {
  51.                     echo $nombre . ", este archivo existe";
  52.         }*/
  53.             } else {
  54.         //echo "archivo no permitido, es tipo de archivo prohibido o excede el tamano de $limite_kb Kilobytes";
  55.                 return null;
  56.             }
  57.         }
  58.     }

y en la vista Vehiculos/add.php:

Código PHP:
Ver original
  1. <h3>Crear Vehículo</h3>
  2. <form method="post" action="index.php?c=vehiculos&a=add" name="frm_addveh">
  3.     <table>
  4.         <tr>
  5.             <td><label for="mat">Matrícula del Vehículo:</label></td>
  6.             <td><input type="text" name="txtmat" id="mat" autofocus required="required" placeholder="Ingrese Matrícula" /></td>
  7.         </tr>
  8.         <tr>
  9.             <td><label for="cant">Cantidad del Vehículo:</label></td>
  10.             <td><input type="text" name="txtcant" id="cant" required="required" placeholder="Ingrese Cantidad" /></td>
  11.         </tr>
  12.         <tr>
  13.             <td><label for="descrip">Descripción del Vehículo:</label></td>
  14.             <td><textarea id="descrip" name="txtdes" rows="10" cols="40"></textarea></td>
  15.         </tr>
  16.         <tr>
  17.             <td><label for="foto">Foto del Vehículo:</label></td>
  18. <!-- esta es la parte de la imagen -->
  19.             <td><input type="file" name="foto" id="foto" required="required" /></td>
  20.         </tr>
  21.         <tr>
  22.             <td>Modelo del Vehículo:</td>
  23.             <td>
  24.                 <input id="mod" list="modelos" required="required" name="txtmod" />
  25.                 <datalist id="modelos">
  26.                     <?php foreach (Session::get('modelos') as $modelo) { ?>
  27.                         <option value=<?php echo $modelo['id'];?> ><?php echo $modelo['marca']." - ".$modelo['nom'];?> </option>
  28.                     <?php }?>
  29.                 </datalist> &nbsp;
  30.                 <input type="button" onclick="frm_addveh.submit();" value="Buscar" />
  31.             </td>
  32.         </tr>
  33.         <tr>
  34.             <td><label for="tipo">Tipo del Vehículo:</label></td>
  35.             <td>
  36.                 <select name="cboxtipo" id="tipo">
  37.                     <?php foreach (Session::get('tiposveh') as $tipoveh) { ?>
  38.                         <option value="<?php echo $tipoveh['id']; ?>"><?php echo $tipoveh['nom']; ?></option>
  39.                     <?php }?>    
  40.                 </select>
  41.             </td>
  42.         </tr>
  43.     </table>
  44.     <p>
  45.         <input type="submit" value="Aceptar" name="btnaceptar" />&nbsp;
  46.         <a href="index.php?c=vehiculos&a=index"><input type="button" value="Cancelar" /></a>
  47.     </p>
  48. </form>
Estuve buscando, probando pero no dí con la respuesta..

Espero sus respuestas y saludos.