Generate new name for the image during upload - php

0

I use the code below and it works correctly, however I need to change the name of the image during UPLOAD.

<?php require_once('conexao.php'); ?>
<?php
$cliente = $_GET['cliente'];
$cod = $_GET['cod'];

$uploaddir = "./clientes/".$cliente."/".$cod."/"; 

$file = $uploaddir . basename($_FILES['uploadfile']['name']); 

$imvfoto = $_FILES['uploadfile']['name'];       

$sqlgravar="INSERT INTO fotos(cod,cliente,foto) 
VALUES ('$cod','$cliente','$imvfoto')"; 
$resultadogravar = mysql_query($sqlgravar)
or die (mysql_error());



if (move_uploaded_file($_FILES['uploadfile']['tmp_name'], $file)) { 
  echo "success"; 
} else {
    echo "error";
}

    include("resize-class.php");
    $resizeObj = new resize("/home/roteirodoimovelc/public_html/cp/clientes/".$cliente."/".$cod."/".$imvfoto."");
    // *** 2) Resize image (options: exact, portrait, landscape, auto, crop)
    $resizeObj -> resizeImage(600, 450, 'exact');
    $resizeObj -> saveImage("/home/roteirodoimovelc/public_html/cp/clientes/".$cliente."/".$cod."/".$imvfoto."", 100);

?>

How do I make this implementation so that the photo is renamed and saved on the server and also in the database?

    
asked by anonymous 30.12.2015 / 17:06

3 answers

4

Just change these lines:

$file = $uploaddir . basename($_FILES['uploadfile']['name']); 
$imvfoto = $_FILES['uploadfile']['name'];

by

$imvfoto = "o nome que você quiser";
$file = $uploaddir . $imvfoto;

If you want to generate a unique ID and get the file extension:

$imvfoto = uniqid('img_').pathinfo($_FILES['uploadfile']['name'], PATHINFO_EXTENSION);
$file = $uploaddir . $imvfoto;

Of course you will adapt this to the desired logic, but since you did not define where the new name comes from in the question, it would be speculation.

In any case, you need to see if this makes sense, as it would be enough to use the bank ID as a new name, for example by filling in zeros. So they would get 0000001, 0000002, etc, even recording the original name in the DB just for reference.

    
30.12.2015 / 17:11
2

You can use UNIX TIMESTAMP

// Define o novo nome do arquivo usando um UNIX TIMESTAMP
$nome = time() . '.' . $extensao;

To get the extension use:

$extensao= $_FILES['uploadfile']['type'];
    
30.12.2015 / 17:23
2

Good afternoon Gladison, when I work with image upload in any language beyond the concern of changing the name I find it interesting you treat the extension so that you do not have future problems with uploading files that could damage your server. This way I work the code this way.

        //verifica que tipo de arquivo esta sendo enviado
        preg_match("/\.(gif|bmp|png|jpg|jpeg){1}$/i", $foto["name"], $ext);
        //altera o nome
        $nome_imagem = md5(uniqid(time())) . "." . $ext[1];
        //define o caminho para qual a imagem será enviada
        $caminho_imagem = "fotos/" . $nome_imagem;
        //efetua o upload
        move_uploaded_file($foto["tmp_name"], $caminho_imagem);

I hope I have helped.

    
30.12.2015 / 17:36