how to upload a file and download it via php?

Asked

Viewed 27 times

0

I created a button to make the ulpoad of a file for the database. up to there beauty. but there is a problem it saves the file with the day and time instead of the file name. I wanted to know if there is a way to save this file with its own name or modify it before uploading. and would like to know how do I download it again, example: I uploaded a medical consultation, and the next appointment 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>

1 answer

2


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

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

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

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

This way, you will find the original name and its extension, leaving the variable $ext worthless.

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

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

This way you can transfer the image.

Link to the property of download.

Browser other questions tagged

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