95 lines
3.3 KiB
PHP
95 lines
3.3 KiB
PHP
<?php
|
|
header('Content-Type: application/json');
|
|
error_reporting(E_ALL);
|
|
ini_set('display_errors', 0);
|
|
|
|
try {
|
|
// Log de inicialização
|
|
file_put_contents('upload_debug.log', "\n" . date('Y-m-d H:i:s') . " - Iniciando upload temporário\n", FILE_APPEND);
|
|
|
|
// 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");
|
|
}
|
|
chmod($temp_dir, 0777);
|
|
}
|
|
|
|
// Verificar upload
|
|
if (!isset($_FILES['document'])) {
|
|
throw new Exception("Nenhum arquivo enviado");
|
|
}
|
|
|
|
// Verificar erros de upload
|
|
if ($_FILES['document']['error'] !== UPLOAD_ERR_OK) {
|
|
$error_messages = [
|
|
UPLOAD_ERR_INI_SIZE => "O arquivo excede o tamanho máximo permitido",
|
|
UPLOAD_ERR_FORM_SIZE => "O arquivo excede o tamanho máximo do formulário",
|
|
UPLOAD_ERR_PARTIAL => "O upload foi feito parcialmente",
|
|
UPLOAD_ERR_NO_FILE => "Nenhum arquivo foi enviado",
|
|
UPLOAD_ERR_NO_TMP_DIR => "Diretório temporário não encontrado",
|
|
UPLOAD_ERR_CANT_WRITE => "Falha ao gravar arquivo",
|
|
UPLOAD_ERR_EXTENSION => "Upload interrompido por extensão"
|
|
];
|
|
|
|
$error_message = isset($error_messages[$_FILES['document']['error']])
|
|
? $error_messages[$_FILES['document']['error']]
|
|
: "Erro desconhecido no upload";
|
|
|
|
throw new Exception($error_message);
|
|
}
|
|
|
|
// Validar tipo de arquivo
|
|
$allowed_types = ['image/jpeg', 'image/png', 'image/jpg'];
|
|
$finfo = finfo_open(FILEINFO_MIME_TYPE);
|
|
$mime_type = finfo_file($finfo, $_FILES['document']['tmp_name']);
|
|
finfo_close($finfo);
|
|
|
|
if (!in_array($mime_type, $allowed_types)) {
|
|
throw new Exception("Tipo de arquivo não permitido. Envie apenas imagens JPG ou PNG");
|
|
}
|
|
|
|
// Gerar nome único
|
|
$extension = pathinfo($_FILES['document']['name'], PATHINFO_EXTENSION);
|
|
$temp_name = uniqid('doc_') . '_' . time() . '.' . $extension;
|
|
$temp_path = $temp_dir . $temp_name;
|
|
|
|
// Mover arquivo
|
|
if (!move_uploaded_file($_FILES['document']['tmp_name'], $temp_path)) {
|
|
throw new Exception("Erro ao mover arquivo para pasta temporária");
|
|
}
|
|
|
|
// Log de sucesso
|
|
file_put_contents('upload_debug.log', "Arquivo movido com sucesso: " . $temp_path . "\n", FILE_APPEND);
|
|
|
|
// Retornar sucesso
|
|
echo json_encode([
|
|
'success' => true,
|
|
'imageUrl' => 'temp/' . $temp_name,
|
|
'message' => 'Upload realizado com sucesso',
|
|
'temp_path' => $temp_path,
|
|
'mime_type' => $mime_type
|
|
]);
|
|
|
|
} catch (Exception $e) {
|
|
// Log de erro
|
|
file_put_contents('upload_debug.log', "ERRO: " . $e->getMessage() . "\n", FILE_APPEND);
|
|
|
|
// Retornar erro
|
|
http_response_code(400);
|
|
echo json_encode([
|
|
'success' => false,
|
|
'message' => $e->getMessage(),
|
|
'debug_info' => [
|
|
'post_data' => $_POST,
|
|
'files_data' => isset($_FILES['document']) ? [
|
|
'name' => $_FILES['document']['name'],
|
|
'type' => $_FILES['document']['type'],
|
|
'error' => $_FILES['document']['error']
|
|
] : 'No file data'
|
|
]
|
|
]);
|
|
}
|
|
?>
|