0
I want to upload files, where I insert the file name into the database and the file into a folder on the server.
I’m trying this way:
HTML:
<form class="form5" method="post" enctype="multipart/form-data">
<div class="row clearfix">
<span class="btn fileinput-button">
<i class="glyphicon glyphicon-plus"></i>
<span>Add Arquivo...</span>
<input type="file" id="arquivo" name="arquivo">
</span>
</div>
</form>
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-dismiss="modal">Cancelar</button>
<button type="button" class="btn btn-success" id="mensagem-sucesso" onclick="inserir_anexos()">Gravar</button>
</div>
JS:
function inserir_anexos()
{
var dadosajax = {
'CodigoUtente' : $("#CodigoUtente6").val(),
'arquivo' : $("#arquivo").val()
};
$.ajax({
url: './recebe_upload',
type: 'POST',
cache: false,
data: dadosajax,
error: function(){
Swal.fire("Erro!", "Tente novamente. Caso persista o erro, contatar Administrador!", "error");
},
success: function(result)
{
$('.form5')[0].reset();
Swal.fire('Boa!', 'Gravado com sucesso!', 'success');
}
});
}
PHP got this way:
$CodigoUtente = mysqli_real_escape_string($conn, $_POST["CodigoUtente"]);
$Colaborador = $_SESSION['usuarioId'];
$pathToSave = "/var/www/html/wp-content/themes/sparkling/alimentacao";
if (!file_exists($pathToSave)) {
mkdir("$pathToSave", 0777);
}
if ($_POST['arquivo']) {
$dir = $pathToSave;
$tmpName = $_POST['arquivo']['tmp_name'];
$name = $_POST['arquivo']['name'];
preg_match_all('/\.[a-zA-Z0-9]+/', $name, $extensao);
if (!in_array(strtolower(current(end($extensao))), array('.txt', '.pdf', '.doc', '.xls', '.xlms'))) {
echo('Permitido apenas arquivos doc,xls,pdf e txt.');
die;
}
if (move_uploaded_file($tmpName, $dir.$name)) {
echo('Arquivo adicionado com sucesso.');
} else {
echo('Erro ao adicionar arquivo.');
}
$query = 'INSERT INTO raddb.UploadArquivo(CodigoUtente, arquivo, Colaborador)
VALUES ( ?, ?, ?)';
$stmt = $conn->prepare( $query );
$stmt->bind_param("sss", $CodigoUtente, $name, $Colaborador);
$stmt->execute();
}
But when I record I get this message on the console:
Allowed only doc,xls, pdf and txt files.
And I’m making insert of a pdsf as I show in the image:
And in the browser console the file name is also sent, as shown in the image:
But neither insert into the database nor store the pdf file in the specified folder.
Can help?
I changed that part of the code, but continues to stop processing on the die
– Junior
yes, but that is expected correct? if the extension is not allowed should stop there, otherwise it will insert an invalid file. Does uploading a file with a valid extension work? In my reply the input name was wrong, see if you are testing with this code that is now pf
– Ricardo Pontual
I am uploading a file with the PDF extension, it is a valid extension, but processing forever in die.
– Junior
@Ricardopunctual,
pathinfo()
returns the file extension without the point what does co what always falls inside the 'if'– Augusto Vasques
you’re right @Augustovasques, I tested here and I was with this problem... I copied this code from a page here, but I pasted the array that was in the question and I didn’t notice it, I will update the answer
– Ricardo Pontual