Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.36 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4.  
  5. //directorio donde se guardara la imagen
  6. $target_dir = "../imagenes/";
  7.  
  8.  
  9.  
  10. //toda la informacion del fichero que voy a subir
  11. $file = $_FILES['imagen'];
  12.  
  13. //la informacion anterior, pero desenglosada
  14. $fileName = $_FILES['imagen']['name'];
  15. $fileTmpName = $_FILES['imagen']['tmp_name'];
  16. $fileSize = $_FILES['imagen']['size'];
  17. $fileError = $_FILES['imagen']['error'];
  18. $fileType = $_FILES['imagen']['type'];
  19.  
  20.  
  21. //obtengo la extensiion (divido el nombre de la extension mediante el punto '.')
  22. $fileExt = explode('.',$fileName);
  23.  
  24. //cojo solo la parte de la extension, y tambien lo pongo todo a minusculas por si a caso
  25. $fileActualExt = strtolower(end($fileExt));
  26.  
  27. $allowed = array('jpg','jpeg','png','pdf');
  28.  
  29. //si el fichero tiene una de las extensiones permitidas
  30. if (in_array($fileActualExt, $allowed)) {
  31. //si no hay errores
  32. if ($fileError === 0 ) {
  33. //si el tamaño es menor x Tamaño
  34. if ($fileSize < 500000 ) {
  35. //le asigno un id unico y le concateno el nombre
  36. $fileNameNew = uniqid('', true). "." . $fileActualExt;
  37. //digo donde se va a guardar el fichero
  38. $fileDestination = $target_dir . $fileNameNew;
  39.  
  40. move_uploaded_file($fileTmpName, $fileDestination);
  41.  
  42. }
  43. else{
  44. echo "Your file is too big";
  45. }
  46. }
  47. //si hubo errores alsubir el fichero
  48. else{
  49. echo "Hubo un error al subir el fichero";
  50. }
  51. }else{
  52. echo "Imagen subida tiene una extension no valida";
  53. }
  54.  
  55.  
  56.  
  57.  
  58.  
  59. ?>
  60.  
  61. <?php
  62. session_start();
  63.  
  64. if (!isset($_SESSION['usuario'])) {
  65. header('location:login.php');
  66. exit();
  67. }
  68. ?>
  69.  
  70. <!DOCTYPE html>
  71. <html lang="en">
  72. <head>
  73. <meta charset="UTF-8">
  74. <meta name="viewport" content="width=device-width">
  75. <title>Inicio</title>
  76. <!-- IMPORT BOOTSRAP-CSS -->
  77. <?php include_once 'partials/bootstrap_css.php' ?>
  78. <!-- CSS 'FULL' -->
  79. <link rel="stylesheet" href="estilos/full.css">
  80. </head>
  81. <body>
  82.  
  83.  
  84. <div class="container">
  85. <?php include_once 'partials/menu.php'; ?>
  86.  
  87.  
  88. <div class="row">
  89. <div class="col">
  90. <!-- CUERPO -->
  91. <form class="tema_principal" method="POST" action="procesos/guardar_mensaje.php" enctype="multipart/form-data">
  92. <div class="form-group" >
  93. <h2>Crear Mensaje </h2>
  94. <?php
  95. if (isset($_SESSION['success'])) {
  96. echo "<h3 style='color:green'>Mensaje creado correctamente!</h3>";
  97. unset($_SESSION['success']);
  98. }
  99.  
  100.  
  101.  
  102. ?>
  103. <label>Titulo</label>
  104. <input type="text" class="form-control" id="titulo" name="titulo" placeholder="Introduce un titulo para el mensaje" style="font-weight: bold;">
  105. </div>
  106. <div class="form-group">
  107. <label>Contenido (max. 500)</label>
  108. <textarea class="form-control" rows="10" name="contenido" placeholder="Introduce el contenido de tu mensaje"></textarea>
  109. </div>
  110.  
  111. <!-- FIle -->
  112. <div class="input-group">
  113. <div class="input-group-prepend">
  114. <span class="input-group-text" id="inputGroupFileAddon01">Imagen</span>
  115. </div>
  116. <div class="custom-file">
  117. <input type="file" class="custom-file-input" id="imagen" aria-describedby="inputGroupFileAddon01" name="imagen">
  118. <label class="custom-file-label" for="inputGroupFile01">Examinar...</label>
  119. </div>
  120. </div>
  121. <!-- FIle -->
  122. <br>
  123.  
  124. <button style="width: 150px;" type="submit" class="btn btn-success">Crear</button>
  125. </form>
  126. </div>
  127. </div>
  128.  
  129. </div>
  130.  
  131.  
  132.  
  133. <!-- IMPORT BOOTSRAP JQUERY -->
  134. <?php include_once 'partials/bootstrap_jquery.php' ?>
  135. </body>
  136. </html>
  137.  
  138. <?php
  139. session_start();
  140.  
  141.  
  142.  
  143. if (isset($_SESSION['usuario'])) {
  144.  
  145. require_once "../conexion.php";
  146. require_once "../controllers/mensaje.php";
  147.  
  148. unset($_SESSION['error']);
  149. unset($_SESSION['success']);
  150.  
  151.  
  152. $titulo = $_POST['titulo'];
  153. $contenido = $_POST['contenido'];
  154. $usuario = $_SESSION['usuario'];
  155.  
  156.  
  157.  
  158. $datos = array(
  159. 'titulo' => $titulo,
  160. 'contenido' => $contenido,
  161. 'usuario' => $usuario
  162. );
  163.  
  164. $mensaje_controller = new Mensaje();
  165.  
  166. $result = $mensaje_controller->crearMensaje($datos['titulo'],$datos['contenido'],$datos['usuario']);
  167.  
  168. if ($result) {
  169. //si se ha guardado ( practicamente siempre)
  170. $_SESSION['success'] ="Mensaje creado correctamente";
  171.  
  172.  
  173. //si todo ha ido bien hasta el momento aqui SUBO LA IMAGEN
  174. //require_once 'subir_imagen.php';
  175. include 'subir_imagen.php';
  176.  
  177. }
  178.  
  179. else{
  180. $_SESSION['error'] = "Ha habido algun error al crear tu mensaje";
  181. }
  182.  
  183. header('location: ../crear_mensaje.php');
  184.  
  185. }
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement