This topic is locked

How to write the contents of a blog field in a folder?

1/23/2026 10:52:48
PHPRunner General questions
J
Jan author

Hi everyone, I built a web application using PHP Runner v 11.2 / MySQL. I created a table that currently contains over 8,000 records, but it also contains four blob fields filled with photos. The database is over 1 GB in size. Yes, I know, it was a foolish decision to do it this way; it's now very slow. It would have been better to store the photos in folders. Ultimately, I have this question: What code can I use to dump the contents of a blog field in a table into a designated folder, so I can then retrieve the same photo from the folder? Thanks in advance.

ChatGPT 1/23/2026

Certainly! To extract the contents of a BLOB field from a MySQL database and save them as files in a designated folder, you can use PHP to do this. Below is a sample code snippet that demonstrates how to retrieve BLOB data from a MySQL table and save it as image files on your server.

Step-by-Step Guide:

  1. Connect to Your Database: Establish a connection to your MySQL database using PHP.


  2. Fetch the BLOB Data: Query the database to retrieve the BLOB data along with any unique identifier (like an ID) to use as the filename.


  3. Save the Files: Write the BLOB data to files in a designated folder.



Sample Code:

<?php
// Database configuration
$servername = "your_server";
$username = "your_username";
$password = "your_password";
$dbname = "your_database";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

// Query to select the BLOB data and an identifier (e.g., id)
$sql = "SELECT id, blob_field FROM your_table"; // Replace 'blob_field' and 'your_table' with your actual field and table names
$result = $conn->query($sql);

if ($result->num_rows > 0) {
// Designated folder to save images
$targetDir = "path/to/your/folder/"; // Ensure this folder exists and is writable

// Loop through the results
while ($row = $result->fetch_assoc()) {
$id = $row['id'];
$blobData = $row['blob_field'];

// Generate a filename (you can customize this)
$fileName = $targetDir . "image_" . $id . ".jpg"; // Change extension based on your image type

// Save the BLOB data to a file
file_put_contents($fileName, $blobData);
}
echo "Images have been successfully saved.";
} else {
echo "No records found.";
}

// Close connection
$conn->close();
?>

Notes:

  • File Permissions: Ensure that the target directory has the appropriate permissions to allow PHP to write files.
  • Image Format: Adjust the filename extension based on the actual format of your images (e.g., .jpg, .png, etc.).
  • Error Handling: You might want to add error handling to check if the file writing was successful.
  • Batch Processing: If you're dealing with a large number of records, consider batching the process or implementing a way to resume if it fails.
  • Backup: Always ensure you have backups before performing operations that modify your data.

Retrieving Photos Later:

Once the images are saved to a folder, you can access them via a URL pointing to the folder on your web server. For example, if your web server URL is http://example.com and you saved images in path/to/your/folder/, you can access an image using https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=100719&image=1&table=forumreplies.

Feel free to modify the code to suit your specific needs!

Sergey Kornilov admin 1/23/2026

You may want to check this:
https://xlinesoft.com/fmm

J
Jan author 1/24/2026

Thanks for the helpful tip. Unfortunately, I'm getting an error message. What am I doing wrong?

img alt

img alt