How to store a PDF file in MySQL database?

How to store a PDF file in MySQL database?

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Storing PDFs in MySQL: A Practical Guide

Storing a PDF file directly inside a MySQL database column is technically feasible, but it is almost never the right architectural choice for production applications. While modern storage engines like InnoDB support the LONGBLOB and TINYBLOB data types, which can theoretically hold binary large objects, loading megabytes of binary data into the database engine introduces significant performance bottlenecks that often outweigh the perceived convenience of keeping files "in one place."

The primary issue lies in how MySQL handles binary data. When you insert a PDF into a LONGBLOB field, that data becomes part of the transaction log and subject to indexing strategies (if columns are included in indexes), backup procedures, and query execution plans. Every time your application queries the database, it must fetch this binary payload from disk or memory before handing it off to the web server. If you are storing thousands of documents, the database size balloons rapidly, increasing I/O wait times and slowing down unrelated read operations on user data.

The Standard Approach: Separate Storage

The industry-standard pattern for handling files like PDFs is to store only a reference (a path or URL) in the database while keeping the actual file in a dedicated file system location or an object storage service. This separation of concerns allows you to optimize your database schema for fast, text-based queries while offloading heavy binary I/O to a system designed for it.

In a typical PHP implementation using Laravel, you would use the Storage facade to write the file to a designated directory, such as storage/app/public/documents. The application then generates a public URL by appending the path to your domain. You store this URL in the database alongside other metadata like title, created_at, and author_id.

use Illuminate\Support\Facades\Storage;

$filePath = Storage::disk('public')->put('documents', $pdfFile->getRealPath());
$path = Storage::url($filePath);

// Store only the path in the database
UserDocument::create([
    'user_id' => $userId,
    'title' => $request->input('title'),
    'file_path' => $path,
]);

This approach ensures that your database remains lean. If you need to backup your database, you can ignore the binary blobs entirely if they are not critical for data integrity, or you can treat the file system as a separate backup target.

When Database Storage Might Make Sense

There are niche scenarios where storing the PDF directly in the database might be justified, primarily when the database serves as a single source of truth for a very small dataset (e.g., a few documents) and the entire application runs on a local development environment or a single-server architecture where I/O contention is not a concern. Even then, modern object storage solutions like AWS S3 or MinIO are often cheaper and more reliable than managing local disk space alongside your database instance.

Retrieval and Security Considerations

Regardless of where the file resides, security remains paramount. If you choose to store files on the web server's filesystem, ensure that the directory permissions are restricted so that only the web server process can read them. Never allow users to upload files directly into directories that serve static assets without validation. PDFs, like any binary file, can contain malicious scripts or exploits if not validated during upload. Always verify the MIME type and check the file extension against a whitelist of allowed types before saving.

If you store the file in cloud storage, leverage their built-in security features, such as IAM roles and access policies, to control who can read or write specific buckets. In Laravel, this might look like configuring an S3 disk:

Storage::disk('s3')->put('documents/filename.pdf', $fileContent);
$path = Storage::url('documents/filename.pdf');

Performance Implications of Blob Columns

If you proceed with storing blobs in MySQL, be aware that SELECT queries will return the file content automatically. This means your application must handle binary data serialization and deserialization logic to serve it to a browser or download it as an attachment. Furthermore, if you join this table with others, the database engine may decide to read the entire row, including the heavy blob, even if you only need the ID. This can lead to unexpected memory spikes in long-running processes.

Ultimately, while MySQL can hold a PDF, separating data from storage is the robust solution that scales with your application's growth.

Our network