How to retrieve/display BLOB attachment in php?

<?php
include('database.php');

$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$typeCode = $_GET['typeCode'];

$stmt = $conn->prepare("SELECT typeCode, fileName, fileContent FROM tbl_doc_files WHERE typeCode = ?");
$stmt->bind_param("s", $typeCode);
$stmt->execute();
$result = $stmt->get_result();

if ($result) {
    if ($result->num_rows > 0) {
        $row = $result->fetch_assoc();
        $fileName = $row['fileName'];
        $fileContent = $row['fileContent'];

        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename="' . $fileName . '"');

        echo $fileContent;
    } else {
        echo "Attachment not found";
    }
} else {
    echo "Error executing the query: " . $conn->error;
}

$stmt->close();
$conn->close();
?>

i can retrieve the fileName but when i am trying to retrieve or display the fileContent it keeps loading. Also there are times that I can download the file but took like 15minutes before downloading.

the fileContent/attachment size was just 2MB.

is there any way?

thank you

The file is probably corrupted.

  1. What is the full code that got the uploaded file from the temporary upload location and inserted the row into the database?
  2. What is the size of the original file that was uploaded?
  3. What is the max_allowed_packet setting on your database server?
  4. What is the database table definition?

Finally, you should store files in the file system. That is what the file system is designed for. Databases are for storing data.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.