69 lines
1.8 KiB
PHP
69 lines
1.8 KiB
PHP
<?php
|
|
// Certificar que nenhuma saída foi enviada antes
|
|
ob_clean();
|
|
ob_start();
|
|
|
|
// Definir cabeçalho JSON
|
|
header('Content-Type: application/json');
|
|
|
|
// Configurações de erro
|
|
error_reporting(E_ALL);
|
|
ini_set('display_errors', 0);
|
|
|
|
// Função para retornar resposta JSON
|
|
function jsonResponse($success, $message, $data = []) {
|
|
echo json_encode(array_merge([
|
|
'success' => $success,
|
|
'message' => $message
|
|
], $data));
|
|
exit;
|
|
}
|
|
|
|
try {
|
|
// Verificar diretório temp
|
|
$temp_dir = __DIR__ . "/temp/";
|
|
if (!file_exists($temp_dir)) {
|
|
if (!mkdir($temp_dir, 0777, true)) {
|
|
throw new Exception("Erro ao criar diretório temporário");
|
|
}
|
|
}
|
|
|
|
// Debug
|
|
error_log("POST: " . print_r($_POST, true));
|
|
error_log("FILES: " . print_r($_FILES, true));
|
|
|
|
// Verificar se recebeu arquivo
|
|
if (empty($_FILES['document'])) {
|
|
throw new Exception("Nenhum arquivo enviado");
|
|
}
|
|
|
|
if ($_FILES['document']['error'] !== UPLOAD_ERR_OK) {
|
|
throw new Exception("Erro no upload: " . $_FILES['document']['error']);
|
|
}
|
|
|
|
// Gerar nome único
|
|
$ext = strtolower(pathinfo($_FILES['document']['name'], PATHINFO_EXTENSION));
|
|
if (!in_array($ext, ['jpg', 'jpeg', 'png'])) {
|
|
throw new Exception("Tipo de arquivo não permitido");
|
|
}
|
|
|
|
$temp_name = 'doc_' . uniqid() . '.' . $ext;
|
|
$temp_path = $temp_dir . $temp_name;
|
|
|
|
// Mover arquivo
|
|
if (!move_uploaded_file($_FILES['document']['tmp_name'], $temp_path)) {
|
|
throw new Exception("Falha ao mover arquivo");
|
|
}
|
|
|
|
// Sucesso
|
|
jsonResponse(true, "Upload realizado com sucesso", [
|
|
'imageUrl' => 'temp/' . $temp_name,
|
|
'tempPath' => $temp_path
|
|
]);
|
|
|
|
} catch (Exception $e) {
|
|
error_log("Erro no upload: " . $e->getMessage());
|
|
jsonResponse(false, $e->getMessage());
|
|
}
|
|
?>
|