Upload different input images using PHP and Mysql

Asked

Viewed 486 times

1

I am developing a PHP system that will generate a Folder to be placed in the shop window. My form is composed of some fields, among them, 4 <input> guy file to upload a featured image and other 3 to Galaria. I created input’s for separate upload and not a Multiple Because I have fields in the database for each image to facilitate the display. My question is: how do I recover these images and record in the comic? So far, I have an idea how to do it with one, but not with several.

Front-End

My back-end:

<?php

include_once 'config/conn.php';

$imagemDestaque = $_FILES["imagemDestaque"];
$textoDestaque  = $_POST["textoDestaque"];
$valor          = $_POST["valor"];
$referencia     = $_POST["referencia"];
$descricao      = $_POST["descricao"];
$galeria1       = $_FILES["galeria1"];
$galeria2       = $_FILES["galeria2"];
$galeria3       = $_FILES["galeria3"];

foreach ($_FILES as $file){
    if($imagemDestaque != NULL) { 
        $nomeFinal = $file['namevb '];
        if(move_uploaded_file($imagemDestaque['tmp_name'], $nomeFinal)) {
            $tamanhoImg = filesize($nomeFinal); 

            $mysqlImg = addslashes(fread(fopen($nomeFinal, "r"), $tamanhoImg));

            try{
            $sql = "INSERT INTO himoveis VALUES (:imagemDestaque,:textoDestaque, :valor, :referencia, :descricao, :galeria1, :galeria2, :galeria3)";
            $q = $conn->prepare($sql);
            $q->execute(array(
                ':imagemDestaque'   =>$imagemDestaque,
                ':textoDestaque'    =>$textoDestaque,
                ':valor'            =>$valor,
                ':referencia'       =>$referencia,
                ':descricao'        =>$descricao,
                ':galeria1'         =>$galeria1,
                ':galeria2'         =>$galeria2,
                ':galeria3'         =>$galeria3
                ));

                echo $sql->rowCount();
            }       
            catch(PDOException $e) {
            echo 'Error: ' . $e->getMessage();

            unlink($nomeFinal);

            header("location:exibir.php");
            }

        }else { 
            echo"Você não realizou o upload de forma satisfatória."; 
        }
    }
}
?>
  • It is not advisable to save images in the BD, you can save the images in a folder and save the link to the folder in the BD.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.