Hello friends.
I am doing a registration process where the form is sent and registered in the database. So far everything is calm. But then I need to send the photo captured by the camera and that's where the error happens.
I found some ideas that use ImagePicker but to avoid fraud you need to take the photo right away. Then, if the information is provided, the data is sent to the server and if it is written successfully, then the image is sent.
When I upload it via curl or an alternative form for testing, everything works normally. Just don't send the photo to the app created with appinventor. Below is the code that receives the file on the server.
<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Allow-Headers: Content-Type");
require_once $_SERVER['DOCUMENT_ROOT'] . '/dbcon.php';
$tipo = isset($_GET['tipo']) ? $_GET['tipo'] : '';
$cpf = isset($_GET['cpf']) ? $_GET['cpf'] : '';
if (!in_array($tipo, ['selfie', 'rg_frente', 'rg_verso'])) {
//die(json_encode(["error" => "Tipo inválido"]));
echo json_encode(array("UPLOAD_DOCS" => "ERROR", "MESSAGE" => "Tipo de documento inválido."));
exit;
}
if (isset($_FILES['file']) && !empty($cpf)) {
$file = $_FILES['file'];
$extensao = pathinfo($file['name'], PATHINFO_EXTENSION);
$nomeArquivo = uniqid($tipo . '_' . $cpf . '_', true) . '.' . $extensao;
$caminhoArquivo = $_SERVER['DOCUMENT_ROOT'] . '/img/users/' . $nomeArquivo;
if (move_uploaded_file($file['tmp_name'], $caminhoArquivo)) {
// Conectar ao banco e atualizar o campo correspondente
try {
$campo = $tipo; // Como os nomes dos campos na tabela são iguais aos tipos
$sql = "UPDATE usuarios SET $campo = :nomeArquivo WHERE cpf = :cpf";
$stmt = $conexao->prepare($sql);
$stmt->bindParam(':nomeArquivo', $nomeArquivo);
$stmt->bindParam(':cpf', $cpf);
if ($stmt->execute()) {
//echo json_encode(["success" => "Arquivo salvo e banco atualizado"]);
echo json_encode(array("UPLOAD_DOCS" => "SUCCESS", "MESSAGE" => "A imagem foi recebida e o banco de dados foi atualizado com sucesso!"));
exit;
} else {
//echo json_encode(["error" => "Falha ao atualizar banco"]);
echo json_encode(array("UPLOAD_DOCS" => "ERROR", "MESSAGE" => "A imagem foi recebida mas ocorreu um erro ao atualizar o banco de dados."));
exit;
}
} catch (PDOException $e) {
//echo json_encode(["error" => "Erro na preparação da query"]);
echo json_encode(array("UPLOAD_DOCS" => "ERROR", "MESSAGE" => "Ocorreu um erro ao atualizar o banco de dados."));
exit;
}
} else {
//echo json_encode(["error" => "Falha ao salvar arquivo"]);
echo json_encode(array("UPLOAD_DOCS" => "ERROR", "MESSAGE" => "Falha ao salvar imagem."));
exit;
}
} else {
//echo json_encode(["error" => "Arquivo ou CPF não informado"]);
echo json_encode(array("UPLOAD_DOCS" => "ERROR", "MESSAGE" => "A imagem ou CPF associado não foi fornecido."));
exit;
}
//$conn->close(); // Removido, pois PDO não precisa de fechamento manual
?>
Blocks:
For some reason that I couldn't figure out, the codeReaponse is not 200. The problem is perhaps with the file path or Header. I don't know for sure. That's why I'm asking for the community's help and I'm grateful to anyone who can provide help.