Upload image to server and get your route

1

I explain the idea and then the situation in which I find myself.

  

The idea is to upload the image obtained through the html form by calling the php on the server, and then (or depending on how you look at it) obtain the path of the image previously loaded on the server in order to perform a SQL query. type INSERT with the path of the image, and the title and content that are strings obtained from the form, all this through a function.

     

The problem is that I can not hang the image on the server or capture its route, besides, it does not show any type of message with echo.

HTML

<form name="menus_create" action="events_create.php" method="post" accept-charset="utf-8" enctype="multipart/form-data">
    <label for="menu_title">Título</label>
    <input name="menu_title" type="text" /></strong><br />
    <label for="menu_content">Contenido</label>
    <textarea name="menu_content"></textarea>​<br />
    <label for="menu_image">Imagen</label>
    <input type="file" name="menu_image"> <br /> <br />
    <input type="submit" name="submit" value="CREAR">
</form>

PHP

<?php 

require_once('database/queries.php');
define("WEB_URL", "http://subdominio.dominio.es/carpeta1/carpeta2/");
define("INSERT_NULL", "No se ha podido continuar la inserción de datos     debido a la falta de algunos.");
define("INSERT_ERROR", "Ha ocurrido un error al intentar insertar nuevos datos en la base de datos.");


function getPathImage($image) {
    if ($_FILES[$image]['error'] > 0) { echo("No ha sido posible cargar la imagen."); }
    else {
        if (in_array($_FILES[$image]['type'], array("image/jpg", "image/jpeg", "image/png")) && $_FILES[$image]['size'] <= 500 * 1024) {
        $path = "images/".$_FILES[$image]['name'];
        echo $path . ''; 
            if (!file_exists($path)) {
                $result = @move_uploaded_file($_FILES[$image]['tmp_name'], $path);
                if (!$result) { return null; }
                else { return $path; }
            }
        } else {
            echo("No ha sido posible cargar la imagen.");
        }
    }
    return null;
}

// AÑADIR MENÚ

if (isset($_POST['menu_create']) != null) {

    $menu_title     = $_POST['menu_title'];
    $menu_content   = $_POST['menu_content'];

    if ($menu_title != null && $menu_content != null) {
        try { Query::addMenu(WEB_URL . getPathImage('menu_image'), $menu_title, $menu_content); }
        catch (PDOException $e) { print_r($e); echo(INSERT_ERROR); }
    } else {
        echo(INSERT_NULL);
    }
}
//header('location: create.php');
?>

PS: Query is the class that used PDO for queries.

    
asked by dddenis 21.03.2016 в 15:27
source

1 answer

-1

The error was in the html, I was trying to capture menu_create when% was name to submit .

Solved.

    
answered by 21.03.2016 / 19:51
source