Errors when inserting images in mysql

Asked

Viewed 108 times

0

Hello, I’d like to insert images into the database for a personal project. The image is sent to the bank yes, but not without showing 3 errors, follow the code and errors :

<?php
$servidor = 'localhost';
$banco    = 'banco_tcc';
$usuario  = 'root';
$senha    = '';
$link     = mysql_connect($servidor, $usuario, $senha);
$db       = mysql_select_db($banco,$link);

if (isset( $_POST['Enviar'])) {

    $foto = $_FILES["foto"];

    if (!empty($foto["name"])) {

        $largura = 2000;
        $altura = 2000;
        $tamanho = 99999999999999999999999999;

        if(preg_match ("/image(jpeg|png)$/", $foto["type"])) { 
           $error[1] = "Isso não é uma imagem.";
        } 

        $dimensoes = getimagesize($foto["tmp_name"]);

        if($dimensoes[0] > $largura) {
            $error[2] = "A largura da imagem não deve ultrapassar ".$largura." pixels";
        }

        if($dimensoes[1] > $altura) {
            $error[3] = "Altura da imagem não deve ultrapassar ".$altura." pixels";
        }

        if($foto["size"] > $tamanho) {
            $error[4] = "A imagem deve ter no máximo ".$tamanho." bytes";
        }

        if (count($error) == 0) {

            preg_match("/.(gif|bmp|png|jpg|jpeg){1}$/i", $foto["name"], $ext);

            $nome_imagem = md5(uniqid(time())) . "." . $ext[1];

            $caminho_imagem = "tmp_name/" . $nome_imagem;

            move_uploaded_file($foto["Fotos"], $caminho_imagem);

            $sql = mysql_query("INSERT INTO ManterGaleria VALUES ('','".$nome_imagem."')");

        }

 if (count($error) != 0) {
            foreach ($error as $erro) {
                echo $erro . " ";
            }
        }
    }
}

Undefined variable:Error in line 52

Undefined index:Photos in line 60

Undefined variable:Error in line 66

How could I fix them without modifying the code completely?

  • 1

    What are the lines cited in the error?

  • if (count($error) == 0) { move_uploaded_file($foto["Fotos"], $caminho_imagem); echo $erro . ";

  • 2

    Recommended Reading: http://answall.com/questions/579/por-que-n%C3%A3o-devo-usar-fun%C3%A7%C3%B5es-do-tipo-mysql

1 answer

2


Undefined variable:Error in line 52

Undefined variable:Error in line 66

It says here that the variable $error not started. Start the array of the variable near the parameters of your sql connection, just above the first if:

$servidor = 'localhost';
$banco    = 'banco_tcc';
$usuario  = 'root';
$senha    = '';
$link     = mysql_connect($servidor, $usuario, $senha);
$db       = mysql_select_db($banco,$link);

$error    =  Array();

Undefined index:Photos in line 60

This error says that the $foto['Fotos'] not defined, verify that in your html the correct name of the upload form’s input and change in the informed line.

  • 1

    I don’t know where this question code comes from, an upload code with variable $error always has scope error. $error only will exist to fall into some otherwise if it will not exist and will only be checked if (count($error) LOL. + 1

Browser other questions tagged

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