Uploading Images with PHP - Mysql

-2

I need to upload an image in MySQL, ie the image path, do you have any practical method to do?

    
asked by anonymous 17.03.2014 / 15:24

3 answers

6

In the official PHP site have this example, which I I changed 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 are going to send to the database is the image path.

    
17.03.2014 / 16:39
1

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

  • Generates thumbnails (in this example one with proportional width of 640px)
  • Rename the file to another name using md5
  • Register in the database (with PDO, in case you do not use PDO put mysql_query or mysqli_query, which will be the case).
  • 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>
    

    Resize.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;
        }
    }
    ?>
    
        
    17.03.2014 / 19:49
    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);
    
        }
    
        
    19.03.2014 / 15:14