Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //directorio donde se guardara la imagen
- $target_dir = "../imagenes/";
- //toda la informacion del fichero que voy a subir
- $file = $_FILES['imagen'];
- //la informacion anterior, pero desenglosada
- $fileName = $_FILES['imagen']['name'];
- $fileTmpName = $_FILES['imagen']['tmp_name'];
- $fileSize = $_FILES['imagen']['size'];
- $fileError = $_FILES['imagen']['error'];
- $fileType = $_FILES['imagen']['type'];
- //obtengo la extensiion (divido el nombre de la extension mediante el punto '.')
- $fileExt = explode('.',$fileName);
- //cojo solo la parte de la extension, y tambien lo pongo todo a minusculas por si a caso
- $fileActualExt = strtolower(end($fileExt));
- $allowed = array('jpg','jpeg','png','pdf');
- //si el fichero tiene una de las extensiones permitidas
- if (in_array($fileActualExt, $allowed)) {
- //si no hay errores
- if ($fileError === 0 ) {
- //si el tamaño es menor x Tamaño
- if ($fileSize < 500000 ) {
- //le asigno un id unico y le concateno el nombre
- $fileNameNew = uniqid('', true). "." . $fileActualExt;
- //digo donde se va a guardar el fichero
- $fileDestination = $target_dir . $fileNameNew;
- move_uploaded_file($fileTmpName, $fileDestination);
- }
- else{
- echo "Your file is too big";
- }
- }
- //si hubo errores alsubir el fichero
- else{
- echo "Hubo un error al subir el fichero";
- }
- }else{
- echo "Imagen subida tiene una extension no valida";
- }
- ?>
- <?php
- session_start();
- if (!isset($_SESSION['usuario'])) {
- header('location:login.php');
- exit();
- }
- ?>
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width">
- <title>Inicio</title>
- <!-- IMPORT BOOTSRAP-CSS -->
- <?php include_once 'partials/bootstrap_css.php' ?>
- <!-- CSS 'FULL' -->
- <link rel="stylesheet" href="estilos/full.css">
- </head>
- <body>
- <div class="container">
- <?php include_once 'partials/menu.php'; ?>
- <div class="row">
- <div class="col">
- <!-- CUERPO -->
- <form class="tema_principal" method="POST" action="procesos/guardar_mensaje.php" enctype="multipart/form-data">
- <div class="form-group" >
- <h2>Crear Mensaje </h2>
- <?php
- if (isset($_SESSION['success'])) {
- echo "<h3 style='color:green'>Mensaje creado correctamente!</h3>";
- unset($_SESSION['success']);
- }
- ?>
- <label>Titulo</label>
- <input type="text" class="form-control" id="titulo" name="titulo" placeholder="Introduce un titulo para el mensaje" style="font-weight: bold;">
- </div>
- <div class="form-group">
- <label>Contenido (max. 500)</label>
- <textarea class="form-control" rows="10" name="contenido" placeholder="Introduce el contenido de tu mensaje"></textarea>
- </div>
- <!-- FIle -->
- <div class="input-group">
- <div class="input-group-prepend">
- <span class="input-group-text" id="inputGroupFileAddon01">Imagen</span>
- </div>
- <div class="custom-file">
- <input type="file" class="custom-file-input" id="imagen" aria-describedby="inputGroupFileAddon01" name="imagen">
- <label class="custom-file-label" for="inputGroupFile01">Examinar...</label>
- </div>
- </div>
- <!-- FIle -->
- <br>
- <button style="width: 150px;" type="submit" class="btn btn-success">Crear</button>
- </form>
- </div>
- </div>
- </div>
- <!-- IMPORT BOOTSRAP JQUERY -->
- <?php include_once 'partials/bootstrap_jquery.php' ?>
- </body>
- </html>
- <?php
- session_start();
- if (isset($_SESSION['usuario'])) {
- require_once "../conexion.php";
- require_once "../controllers/mensaje.php";
- unset($_SESSION['error']);
- unset($_SESSION['success']);
- $titulo = $_POST['titulo'];
- $contenido = $_POST['contenido'];
- $usuario = $_SESSION['usuario'];
- $datos = array(
- 'titulo' => $titulo,
- 'contenido' => $contenido,
- 'usuario' => $usuario
- );
- $mensaje_controller = new Mensaje();
- $result = $mensaje_controller->crearMensaje($datos['titulo'],$datos['contenido'],$datos['usuario']);
- if ($result) {
- //si se ha guardado ( practicamente siempre)
- $_SESSION['success'] ="Mensaje creado correctamente";
- //si todo ha ido bien hasta el momento aqui SUBO LA IMAGEN
- //require_once 'subir_imagen.php';
- include 'subir_imagen.php';
- }
- else{
- $_SESSION['error'] = "Ha habido algun error al crear tu mensaje";
- }
- header('location: ../crear_mensaje.php');
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement