How to upload an image, but save it with the name based on customer data?

Asked

Viewed 37 times

-1

I would like the user to upload his photo, but when saving, I would like it to be saved with the same name as the client’s CPF.

Today I can save the image in a folder, but it comes with the name given by the client.

PHP that saves the image in the destination folder I chose:

<?php
$cpf   = $_SESSION['cpf'];

$nome_temporario=$_FILES["Arquivo"]["tmp_name"];
$nome_real=$_FILES["Arquivo"]["name"];
copy($nome_temporario,"imagens/$nome_real");
?>

HTML of the page:

<form action="envia_arquivo.php" method="post" enctype="multipart/form-data">
<div>
    <label id="ftbt" for='arquivo'><img src="../FORM/Upload.svg"></label>
    <input name="Arquivo" id='arquivo' type='file' required accept="image/*">
    <button class="botao1" type="submit" value="Continuar">Continuar</button>
</div>
</form>

I’ve searched everything that is singing and I can’t find a PHP shape that works, most I tried didn’t send the photo to the folder.

2 answers

0


copy($nome_temporario,"imagens/$cpf");

Being $cpf a variable that you need to declare and set before, then this value can come from wherever you want , bank, prorio form, session etc....

  • error: Warning: copy(): The Second argument to copy() Function cannot be a directory in *** line 6

  • @Pedrocastilho in this case probably the value of $_SESSION['cpf']; this empty, check it, or if you want to test leave it so $cpf = 'testando_cpf'

  • vdd... with the 'testando_cpf' was correctly... thank you

  • apparently it was just that msm... the Septssion ta empty

0

You can rename the image when moving it to the server.

$temp = explode(".", $_FILES['Arquivo']['name']); //Usado para pegar a extensão da imagem

$novo_nome = $_SESSION['CPF'].".".end($temp); //concatena o a variavel do novo nome com a extensão da imagem cpf+.+extensão

if(move_uploaded_file($_FILES['Arquivo']['tmp_name'], "imagem/".$novo_nome)){
     //código caso upload tenha sucesso
 }
  • yours showed no error, however when entering the folder, tbm did not send it there

  • I made a correction that was missing to capture the image extension, now it is with the same code I used here, always check the name you have in $_FILES and the variable of SESSION if they are correct.

Browser other questions tagged

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