'El archivo no es una imagen válida.']); exit(); } // Allow certain file formats $allowed_types = ['jpg', 'png', 'jpeg', 'gif', 'webp']; if(!in_array($imageFileType, $allowed_types)) { echo json_encode(['error' => 'Formato de archivo no permitido. Solo se aceptan JPG, PNG, GIF, WEBP.']); exit(); } // Check if file already exists if (file_exists($target_file)) { echo json_encode(['error' => 'Ya existe un archivo con este nombre. Por favor, cambia el nombre del archivo o sube uno diferente.']); exit(); } // Try to upload file if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) { // Use the url() helper to create the full, correct URL $file_url = url('galeria/' . $filename); echo json_encode(['url' => $file_url]); } else { echo json_encode(['error' => 'Error al mover el archivo subido.']); } } else { echo json_encode(['error' => 'Solicitud no válida.']); } ?>