File "subirComprobante.php"

Full Path: C:/wamp64/www/RegistroEquipos2/backend/envios/subirComprobante.php
File size: 1.92 KB
MIME-type: text/x-php
Charset: utf-8

<?php
session_start();
header('Content-Type: application/json');

$host = '192.200.100.40';
$db = 'sistemas';
$user = 'SANMARINO';
$pass = 'sanmarino2021*';
$charset = 'utf8mb4';

try {
    $pdo = new PDO("mysql:host=$host;dbname=$db;charset=$charset", $user, $pass, [
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_EMULATE_PREPARES => false
    ]);
} catch (PDOException $e) {
    echo json_encode(['status' => 'error', 'mensaje' => 'Error de conexión: ' . $e->getMessage()]);
    exit;
}

if (!isset($_POST['id']) || empty($_POST['id'])) {
    echo json_encode(['status' => 'error', 'mensaje' => 'ID de registro no proporcionado.']);
    exit;
}
$id = intval($_POST['id']);

if (!isset($_FILES['pdf_file']) || $_FILES['pdf_file']['error'] !== UPLOAD_ERR_OK) {
    echo json_encode(['status' => 'error', 'mensaje' => 'Debes seleccionar un archivo PDF válido.']);
    exit;
}

$archivo = $_FILES['pdf_file'];

$finfo = finfo_open(FILEINFO_MIME_TYPE);
$tipo = finfo_file($finfo, $archivo['tmp_name']);
finfo_close($finfo);

if ($tipo !== 'application/pdf') {
    echo json_encode(['status' => 'error', 'mensaje' => 'Solo se permiten archivos PDF.']);
    exit;
}

if ($archivo['size'] > 5 * 1024 * 1024) {
    echo json_encode(['status' => 'error', 'mensaje' => 'El archivo no debe superar los 5 MB.']);
    exit;
}

$pdfContent = file_get_contents($archivo['tmp_name']);
if ($pdfContent === false) {
    echo json_encode(['status' => 'error', 'mensaje' => 'Error al leer el archivo.']);
    exit;
}

// Nombre de tabla corregido: registrosenvios
$sql = "UPDATE registrosenvios SET pdf_comprobante = :pdf WHERE id = :id";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':pdf', $pdfContent, PDO::PARAM_LOB);
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
$stmt->execute();

echo json_encode(['status' => 'success', 'mensaje' => 'Comprobante subido correctamente a la base de datos.']);