Pergunta

i have a file to modify my profile but i need name change of the imagem during the upload. I need this to be no substitutions images already in the folder when I upload.

Code of my file is:

<?php
include 'verificar.php';
require_once 'config.php';

$id = $_GET["id"];
$nome    = $_POST["nome_perfil"];
$email   = $_POST["email_perfil"];

// Configuração da Imagem
$imagem  = $_FILES["imagem_perfil"];
$destino = "img/perfil/".$imagem['name'];

if(isset($_POST["submit"])){

    if(empty($nome) || empty($email))
    {
        header ("Location: index.php?page=erro");
    }
    elseif(empty($_FILES['imagem_perfil']['name'])) 
    {
        mysql_query("UPDATE utilizadores SET Nome='$nome', Email='$email' WHERE ID='$id'");
        header ("Location: perfil_completo.php");
    }

    else
    {

        mysql_query("UPDATE utilizadores SET Nome='$nome', Email='$email', imgPerfil='".$imagem['name']."' WHERE ID='$id'");

        if($imagem['type'] == "image/jpeg"){
            move_uploaded_file($imagem['tmp_name'] , $destino);
        }else if($imagem['type'] == "image/png"){
            move_uploaded_file($imagem['tmp_name'] , $destino);
        }else if($imagem['type'] == "image/gif"){
            move_uploaded_file($imagem['tmp_name'] , $destino); 
        }
        header ("Location: perfil_completo.php");
    }
}
?>

Now the solution:

<?php
include 'verificar.php';
require_once 'config.php';

$id = $_GET["id"];
$nome    = $_POST["nome_perfil"];
$email   = $_POST["email_perfil"];

 // Configuração da Imagem
 $imagem  = $_FILES["imagem_perfil"];
 $array = explode(".",$imagem['name']);
 $destino = "img/perfil/".$id.".".end($array);

 if(isset($_POST["submit"])){

 if(empty($nome) || empty($email))

     {
     header ("Location: index.php?page=erro");
     }

 elseif(empty($_FILES['imagem_perfil']['name'])) 
 {
     mysql_query("UPDATE utilizadores SET Nome='$nome', Email='$email' WHERE ID='$id'");
     header ("Location: perfil_completo.php");
 }

     else
     {

 mysql_query("UPDATE utilizadores SET Nome='$nome', Email='$email', imgPerfil='".$id.".".end($array)."' WHERE ID='$id'");

 if($imagem['type'] == "image/jpeg"){
    move_uploaded_file($imagem['tmp_name'] , $destino);
 }else if($imagem['type'] == "image/png"){
    move_uploaded_file($imagem['tmp_name'] , $destino);
 }else if($imagem['type'] == "image/gif"){
    move_uploaded_file($imagem['tmp_name'] , $destino); 
 }
header ("Location: perfil_completo.php");
     }
}
?>
Foi útil?

Solução

you can change this:

$destino = "img/perfil/".$imagem['name'];

to this:

$result = mysql_query("Select Nome from utilizadores WHERE ID='$id'");
$result = mysql_fetch_assoc($result);
$yourName = $result['Nome'];

$array = explode(".",$imagem['name']);
$destino = "img/perfil/".$yourName.".".end($array);

The explode() is to get the image sufix.


You can set the name into the $yourName variable

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top