File "test_blob_simple.php"

Full Path: C:/wamp64/www/Formaciones/test_blob_simple.php
File size: 4.01 KB
MIME-type: text/x-php
Charset: utf-8

<?php
// test_blob_simple.php
require_once 'Configuracion/db.php';

echo "<h2>Prueba Básica de Inserción BLOB</h2>";

// Primero, obtener un course_id válido
$curso_result = $mysqli->query("SELECT id FROM courses LIMIT 1");
if ($curso_result && $curso_result->num_rows > 0) {
    $curso_row = $curso_result->fetch_assoc();
    $course_id_valido = $curso_row['id'];
    echo "<p>✅ Usando course_id válido: $course_id_valido</p>";
} else {
    die("<p style='color: red;'>❌ No hay cursos en la base de datos. Crea al menos un curso primero.</p>");
}

// Crear datos de prueba
$test_content = "Este es un contenido de prueba: " . str_repeat("ABCDEFGHIJ", 100);
$test_size = strlen($test_content);

echo "<p>Tamaño del contenido de prueba: $test_size bytes</p>";

// Método 1: Query directa con escape
echo "<h3>Método 1: Query directa</h3>";
$escaped_content = $mysqli->real_escape_string($test_content);
$sql = "INSERT INTO materials (course_id, title, type, file_path, file_name, file_type, file_size, namePro, firmaPro, created_at) 
        VALUES ($course_id_valido, 'TEST DIRECTO', 'file', '$escaped_content', 'test.txt', 'text/plain', $test_size, 'TEST', 'test', NOW())";

if ($mysqli->query($sql)) {
    $test_id = $mysqli->insert_id;
    echo "<p style='color: green;'>✅ Inserción exitosa (ID: $test_id)</p>";
    
    // Verificar
    $check = $mysqli->query("SELECT LENGTH(file_path) as size, file_path FROM materials WHERE id = $test_id");
    $row = $check->fetch_assoc();
    
    echo "<p>Tamaño guardado: " . $row['size'] . " bytes</p>";
    
    if ($row['size'] == $test_size) {
        echo "<p style='color: green;'>✅ El contenido se guardó correctamente</p>";
        
        // Verificar que el contenido es igual
        if ($row['file_path'] === $test_content) {
            echo "<p style='color: green;'>✅✅✅ El contenido es idéntico al original - ¡BLOB FUNCIONA!</p>";
        } else {
            echo "<p style='color: orange;'>⚠️ El contenido difiere del original</p>";
        }
    } else {
        echo "<p style='color: red;'>❌ El tamaño no coincide. Esperado: $test_size, Obtenido: " . $row['size'] . "</p>";
    }
    
    // Limpiar
    $mysqli->query("DELETE FROM materials WHERE id = $test_id");
    echo "<p>Registro de prueba eliminado.</p>";
} else {
    echo "<p style='color: red;'>❌ Error: " . $mysqli->error . "</p>";
}

// Método 2: Prepared statement con bind_param
echo "<hr><h3>Método 2: Prepared Statement</h3>";
$sql2 = "INSERT INTO materials (course_id, title, type, file_path, file_name, file_type, file_size, namePro, firmaPro, created_at) 
         VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, NOW())";

$stmt = $mysqli->prepare($sql2);
$title = "TEST PREPARED";
$type = "file";
$fname = "test2.txt";
$ftype = "text/plain";
$pro = "TEST";
$firma = "test";

$stmt->bind_param("issssssss", $course_id_valido, $title, $type, $test_content, $fname, $ftype, $test_size, $pro, $firma);

if ($stmt->execute()) {
    $test_id2 = $mysqli->insert_id;
    echo "<p style='color: green;'>✅ Inserción exitosa (ID: $test_id2)</p>";
    
    $check2 = $mysqli->query("SELECT LENGTH(file_path) as size FROM materials WHERE id = $test_id2");
    $row2 = $check2->fetch_assoc();
    
    echo "<p>Tamaño guardado: " . $row2['size'] . " bytes</p>";
    
    if ($row2['size'] == $test_size) {
        echo "<p style='color: green;'>✅✅✅ El contenido se guardó correctamente con prepared statement - ¡FUNCIONA!</p>";
    } else {
        echo "<p style='color: red;'>❌ El tamaño no coincide. Esperado: $test_size, Obtenido: " . $row2['size'] . "</p>";
    }
    
    $mysqli->query("DELETE FROM materials WHERE id = $test_id2");
    echo "<p>Registro de prueba eliminado.</p>";
} else {
    echo "<p style='color: red;'>❌ Error: " . $stmt->error . "</p>";
}

$stmt->close();

echo "<hr>";
echo "<h3>📋 Conclusión:</h3>";
echo "<p>Si ambos métodos muestran ✅✅✅, significa que MySQL puede guardar BLOBs correctamente.</p>";
echo "<p>El problema entonces está en cómo se está pasando el archivo desde el formulario.</p>";
?>