I'm trying to send files without using the submit
of the form. I have verified that%% of% is possible, but I have a problem.
Apparently, the file is forwarded, since with the variable " formData
" I can get the data from the file, however I can not direct it to a page with path_parts
.
Form php:
<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 moveupload
or _POST
instead of _FILES
, and in no way do I get the expected result.