how to upload a file and download it through php?

0

I created a button to make the ulpoad of a file for the database. even beauty but has a problem it saves the file with the day and time instead of the file name. was wondering if there is a way to save this file with its own name or modify it before uploading. and I would like to know how to lower it again, eg: I uploaded a medical consultation, and in the next query I would like to download it.

    <?phpif(isset($_FILES['fileUpload'])){
  date_default_timezone_set("Brazil/East"); //Definindo timezone padrão

  $ext = strtolower(substr($_FILES['fileUpload']['name'],-4)); //Pegando extensão do arquivo
  $new_name = date("Y.m.d-H.i.s") . $ext; //Definindo um novo nome para o arquivo
  $dir = 'uploads/'; //Diretório para uploads

  move_uploaded_file($_FILES['fileUpload']['tmp_name'], $dir.$new_name); //Fazer upload do arquivo}?>


        <form action="#" method="POST" enctype="multipart/form-data">
          <input type="file" name="fileUpload">
          <input type="submit" value="Enviar">
       </form>
    
asked by anonymous 02.11.2017 / 20:24

1 answer

2

If you want to get the name, you have the wrong code.

$new_name = date("Y.m.d-H.i.s") . $ext;

In this line above, you give the file name with the system date and time. To put the right name, I recommend using the following code

$new_name = $_FILES['fileUpload']['name'];

This will fetch the original name and its extension, leaving the $ext variable useless.

Now to download, if the file is an image, simply indicate the path of that image in <a> , so

<a href="seusite/imagens/suaimagem.jpg" download>DOWNLOAD</a> 

This will allow you to transfer the image.

download property link.

    
02.11.2017 / 20:43