121 lines
3.5 KiB
PHP
121 lines
3.5 KiB
PHP
<?php
|
|
// Certificar que nenhuma saída foi enviada antes
|
|
ob_clean();
|
|
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 {
|
|
// Configurar diretório temporário com caminho absoluto
|
|
$temp_dir = __DIR__ . "/temp/";
|
|
$web_temp_dir = "temp/"; // Para URLs
|
|
|
|
// Criar diretório se não existir
|
|
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); // Garantir permissões
|
|
}
|
|
|
|
// Debug
|
|
error_log("POST: " . print_r($_POST, true));
|
|
error_log("FILES: " . print_r($_FILES, true));
|
|
|
|
// Verificar se recebeu arquivos
|
|
if (empty($_FILES['document'])) {
|
|
throw new Exception("Nenhum arquivo enviado");
|
|
}
|
|
|
|
$files = [];
|
|
$uploaded_files = [];
|
|
|
|
// Reorganizar array de arquivos
|
|
foreach ($_FILES['document']['name'] as $key => $name) {
|
|
$files[] = [
|
|
'name' => $_FILES['document']['name'][$key],
|
|
'type' => $_FILES['document']['type'][$key],
|
|
'tmp_name' => $_FILES['document']['tmp_name'][$key],
|
|
'error' => $_FILES['document']['error'][$key],
|
|
'size' => $_FILES['document']['size'][$key]
|
|
];
|
|
}
|
|
|
|
// Processar cada arquivo
|
|
foreach ($files as $index => $file) {
|
|
if ($file['error'] !== UPLOAD_ERR_OK) {
|
|
continue;
|
|
}
|
|
|
|
// Validar tipo
|
|
$finfo = finfo_open(FILEINFO_MIME_TYPE);
|
|
$mime_type = finfo_file($finfo, $file['tmp_name']);
|
|
finfo_close($finfo);
|
|
|
|
if (!in_array($mime_type, ['image/jpeg', 'image/png', 'image/jpg'])) {
|
|
continue;
|
|
}
|
|
|
|
// Gerar nome único para cada arquivo
|
|
$ext = pathinfo($file['name'], PATHINFO_EXTENSION);
|
|
$basename = uniqid('doc_') . '_' . time();
|
|
$filename = sprintf('%s_%d.%s', $basename, $index, $ext);
|
|
|
|
$temp_path = $temp_dir . $filename;
|
|
$web_path = $web_temp_dir . $filename;
|
|
|
|
// Mover arquivo
|
|
if (move_uploaded_file($file['tmp_name'], $temp_path)) {
|
|
// Ajustar permissões do arquivo
|
|
chmod($temp_path, 0644);
|
|
|
|
$uploaded_files[] = [
|
|
'url' => $web_path,
|
|
'path' => $temp_path,
|
|
'index' => $index
|
|
];
|
|
}
|
|
}
|
|
|
|
if (empty($uploaded_files)) {
|
|
throw new Exception("Nenhum arquivo válido foi enviado");
|
|
}
|
|
|
|
// Debug
|
|
error_log("Arquivos processados: " . print_r($uploaded_files, true));
|
|
|
|
// Retornar sucesso com primeiro arquivo como principal
|
|
echo json_encode([
|
|
'success' => true,
|
|
'message' => count($uploaded_files) . ' arquivo(s) processado(s)',
|
|
'imageUrl' => $uploaded_files[0]['url'],
|
|
'allImages' => $uploaded_files,
|
|
'fileCount' => count($uploaded_files),
|
|
'baseUrl' => $web_temp_dir,
|
|
'basename' => $basename
|
|
]);
|
|
|
|
} catch (Exception $e) {
|
|
error_log("Erro no upload: " . $e->getMessage());
|
|
echo json_encode([
|
|
'success' => false,
|
|
'message' => $e->getMessage(),
|
|
'debug' => [
|
|
'POST' => $_POST,
|
|
'FILES' => isset($_FILES['document']) ? $_FILES['document'] : 'No files'
|
|
]
|
|
]);
|
|
}
|
|
?>
|