First you will have to upload, if successful just display a link pointing to where the file was stored.
I saw that your code has some validations in javascript, below is an example "CRU", so you can use it as a reference.
Create a file called upload.php, and put the code below:
<html>
<head></head>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="arquivo"/>
<input type="submit" name="btnEnviar" value="Enviar" />
</form>
<?php
if($_POST)
{
$origem = $_FILES['arquivo']['name'];
$diretorio = 'uploads';
$destino = $diretorio.'/'.$origem;
if(!file_exists($diretorio))
{
mkdir($diretorio);
}
if (move_uploaded_file($_FILES['arquivo']['tmp_name'], $destino))
{
echo "<a href='{$destino}'> Baixar Arquivo / Visualizar Arquivo </a>";
} else {
echo "Possível ataque de upload de arquivo!\n";
}
}
?>
</body>
</html>
Explaining
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="arquivo"/>
<input type="submit" value="Enviar" />
</form>
In the form the enctype="multipart / form-data" has been added because it is a file upload;
A file input field called file was also created;
When you click the submit button, the form is forwarded to the same page, then checked to see if $ _POST is true:
OBS ::. The $ _POST is true, when a form is submitted and its method is post
Knowing that POST is true, the file name is assigned the $ source variable:
$origem = $_FILES['arquivo']['name'];
Next in the directory variable the location where the files are to be written is informed, and in the $ target variable the directory with the original file name is concatenated:
$diretorio = 'uploads';
$destino = $diretorio.'/'.$origem;
It then checks whether the directory where the files will be written actually exists; if it does not exist, create the directory using the mkdir command:
if(!file_exists($diretorio))
{
mkdir($diretorio);
}
In the final code snippet, the native PHP function move_uploaded_file is used, informing in the first parameter the temporary path of the file and in the second its destination, if the upload is successful, a download link / visualization of the file is displayed (as we are not forcing the download, who will set whether the file will be displayed or downloaded will be the browser)
if (move_uploaded_file($_FILES['arquivo']['tmp_name'], $destino))
{
echo "<a href='{$destino}'> Baixar Arquivo / Visualizar Arquivo </a>";
} else {
echo "Possível ataque de upload de arquivo!\n";
}
If you want to know all the properties of the file sent through the form, inside the if ($ _ POST) ... add the following code:
<?php
if($_POST)
{
echo "<pre>";
var_dump($_FILES);
echo "<pre>";
... restante do código
The output will be something like:
rray(1) {
["arquivo"]=>
array(5) {
["name"]=>
string(8) "xxxx.csv"
["type"]=>
string(24) "application/vnd.ms-excel"
["tmp_name"]=>
string(24) "C:\xampp\tmp\phpD4C8.tmp"
["error"]=>
int(0)
["size"]=>
int(30)
}
}