Upload Images with PHP - Mysql

Asked

Viewed 5,038 times

-2

I need to upload an image in Mysql, IE, the image path, has some practical method of doing?

  • 3

    Which method have you tried? could describe this approach?

3 answers

6

In the official PHP website have this example, that I altered a little to put :

<?php
// Nas versões do PHP anteriores a 4.1.0, deve ser usado $HTTP_POST_FILES
// ao invés de $_FILES.

$uploaddir = '/var/www/uploads/';
$uploadfile = $uploaddir . $_FILES['userfile']['name'];
print "<pre>";
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploaddir . $_FILES['userfile']['name'])) {
    // Chame aqui um insert no banco de dados. 
    // Deve ser salvo na coluna do banco a seguinte informação: $uploaddir . $_FILES['userfile']['name'])
    print "O arquivo é valido e foi carregado com sucesso. Aqui esta alguma informação:\n";
    print_r($_FILES);
} else {
    print "Possivel ataque de upload! Aqui esta alguma informação:\n";
    print_r($_FILES);
}
print "</pre>";
?>

In this example, the only thing you will send to the database is the image path.

1

public function uploadImage($tmp, $nome, $width, $pasta) {
       $ext = strtolower(substr($nome, -3));
       switch ($ext){
           case 'jpg'  : $img = imagecreatefromjpeg($tmp);break;
           case 'jpeg' : $img = imagecreatefromjpeg($tmp);break;
           case 'png'  : $img = imagecreatefrompng($tmp);break;
           case 'gif'  : $img = imagecreatefromgif($tmp);break;
       }
       $x = imagesx($img);
       $y = imagesy($img);
       $height = ($width * $y) / $x;
       $nova = imagecreatetruecolor($width, $height);

       imagealphablending($nome, false);
       imagesavealpha($nome, false);
       imagecopyresampled($nome,$img, 0, 0, 0, 0, $width, $height, $x, $y);

       switch ($ext){
           case 'jpg'  : imagejpeg($nova, $pasta.nome, 100);break;
           case 'jpeg' : imagejpeg($nova, $pasta.nome, 100);break;
           case 'png'  : imagepng($nova, $pasta.nome);break;
           case 'gif'  : imagegif($nova, $pasta.nome, 100);break;
       }

       imagedestroy($img);
       imagedestroy($nova);

    }

1

I like to use a php class to upload and at the same time resize the image. See if this example is for you. Specifications:

  1. Generates thumbnails (in this example one with 640px proportional width)
  2. Renames the file to another name using md5
  3. Register in the database (with PDO, in case of not using PDO put mysql_query or mysqli_query, what to see the case).
  4. Can be customized

index php.

<?php
include_once ("Redimensiona.php");

if (isset($_POST['acao']) && $_POST['acao']=="cadastrar"){

    $foto = $_FILES['foto'];    
    $redim = new Redimensiona();
    $src=$redim->Redimensionar($foto, 640, "images");

        $cadastra = $conn->prepare("INSERT INTO tabela (campo) VALUES ($src) ");
        $cadastra->execute();

}

?>
<html>
<head>
<title>Teste</title>
</head>
<body>
<form method="post" action="" enctype="multipart/form-data">
    <label>Foto <input type="file" name="foto" /></label>    
    <input type="submit" value="Enviar" />
    <input type="hidden" name="acao" value="cadastrar" />
</form>
<?php
if (isset($_POST['acao']) && $_POST['acao']=="cadastrar"){
    echo "<img src=\"$src\">";
}
?>
</body>
</html>

Resizes.php

<?php
class Redimensiona{

    public function Redimensionar($imagem, $largura, $pasta){

        $name = md5(uniqid(rand(),true));

        if ($imagem['type']=="image/jpeg"){
            $img = imagecreatefromjpeg($imagem['tmp_name']);
        }else if ($imagem['type']=="image/gif"){
            $img = imagecreatefromgif($imagem['tmp_name']);
        }else if ($imagem['type']=="image/png"){
            $img = imagecreatefrompng($imagem['tmp_name']);
        }
        $x   = imagesx($img);
        $y   = imagesy($img);
        $autura = ($largura * $y)/$x;

        $nova = imagecreatetruecolor($largura, $autura);
        imagecopyresampled($nova, $img, 0, 0, 0, 0, $largura, $autura, $x, $y);

        if ($imagem['type']=="image/jpeg"){
            $local="$pasta/$name".".jpg";
            imagejpeg($nova, $local);
        }else if ($imagem['type']=="image/gif"){
            $local="$pasta/$name".".gif";
            imagejpeg($nova, $local);
        }else if ($imagem['type']=="image/png"){
            $local="$pasta/$name".".png";
            imagejpeg($nova, $local);
        }       

        imagedestroy($img);
        imagedestroy($nova);    

        return $local;
    }
}
?>
  • I forgot to reinforce the use of the <meta charset="utf-8"/> tag to avoid coding problems, use the form file.

Browser other questions tagged

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