Rename image before upload

-1

I want to know how to rename imgs before uploading, because of security issues, it's good to rename with date (I think), but if anyone knows a better way please comment.

<?php
include "conexao.php";
include "executaSQL.php";

$nome = $_POST['nome'];
$email = $_POST['email'];
$senha = $_POST['senha'];
$tipo = $_POST['tipo'];
$permissoes = $_POST['permissoes'];

$senha_codificada = sha1($senha);

$arqName = $_FILES['arquivo']['name'];

$arqType = $_FILES['arquivo']['type'];

$arqSize = $_FILES['arquivo']['size'];

$arqTemp = $_FILES['arquivo']['tmp_name'];

$arqError = $_FILES['arquivo']['error'];

list($largura, $altura) = getimagesize($_FILES["arquivo"]["tmp_name"]);

if($largura=="" || $altura ==""){
echo "<script>alert('Você deve selecionar uma imagem válida para envio!')</script>";
echo "<script>window.location.href='cadastro_usuario.php' </script>";
exit;

}
$pasta = 'upload/avatar/';

if(!file_exists($pasta))
    {
        mkdir("$pasta", 0755);
    }
$upload = move_uploaded_file($arqTemp, $pasta . $arqName);

$SQL = "INSERT INTO tbl_usuario VALUES('null','$nome','$email','$senha_codificada','$tipo','$permissoes','$arqName');";

$link= conectar ();
$inserido = executaSQL($SQL, $link);

if ( $inserido == True && $arqError  ==  0){
        echo "<script>alert('Dados Cadastrados com Sucesso!');</script>";
        echo "<script>window.location.href='cadastro_usuario.php' </script>";   
    }
    else{
        echo "<script>alert('Falha ao Realizar Cadastro!');</script>";
        echo "<script>window.location.href='cadastro_usuario.php' </script>";       
}

? >

    
asked by anonymous 07.02.2017 / 12:55

1 answer

3

Replace your $arqName = $_FILES['arquivo']['name']; with:

$ext = pathinfo($_FILES['arquivo']['name'], PATHINFO_EXTENSION);
$arqName = date("Y-m-d-h-i-s").".".$ext;

So you rename with Data and already save the Named Image in SQL.

    
07.02.2017 / 14:22