0
I am trying to send files without using the submit
of the form. I checked that through the formData
It’s possible, but I have a problem.
Apparently, the file is forwarded, since with the variable "path_parts
" I can take the data from the file, but I can’t direct it to a page with the moveupload
.
Php form:
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Ajax upload form</title>
<script>
var form = document.getElementById('the-form');
form.onsubmit = function() {
var formData = new FormData(form);
formData.append('arquivo', arquivo);
var xhr = new XMLHttpRequest();
xhr.open('GET', form.getAttribute('action'), true);
xhr.send(formData);
return false; // To avoid actual submission of the form
}
</script>
</head>
<body>
<!-- The HTML -->
<form id="the-form" action="/portal/teste/upload.php"enctype="multipart/form-data">
<input name="arquivo" type="file">
<input type="submit" value="Upload" />
</form>
</body>
</html>
Page that receives the file:
<?php
$arquivo = $_GET['arquivo'];
$path_parts = pathinfo($arquivo);
$arquivo_tmp = $_FILES[ 'arquivo' ][ 'tmp_name' ];
$novoNome = 'arqteste';
$destino = 'imagens / ' . $novoNome;
echo $path_parts['basename'];
echo '<br>';
echo $path_parts['extension'];
if ( @move_uploaded_file ( $arquivo_tmp, $destino ) ) {
echo 'Arquivo salvo com sucesso em : <strong>' . $destino . '</strong><br />';
echo ' < img src = "' . $destino . '" />';
}else{
echo 'Erro ao salvar o arquivo.<br />';
}
?>
I tried to use _POST
or _FILES
in place of get
, and in no way get the expected result.
take this one off
@
from the beginning of move_uploaded_file.– Wagner Fillio
Also do a validation before. if(isset($_FILES['file']['name'])){ echo 'ok'} Else {echo 'empty'};
– Wagner Fillio