From 8cae37f5df9111de0ad26a3fdc8fe92e072fa606 Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Thu, 28 Jun 2012 17:41:59 -0400
Subject: [PATCH] Write file contents to MySQL database in chunks

This overcomes the eventual limit of and database to support queries of a
finite length. We now split the file contents into 100k chunks and append
the chunks to the database one chunk at a time.
---
 include/class.file.php | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/include/class.file.php b/include/class.file.php
index b9bfbe1b7..891a8b5bf 100644
--- a/include/class.file.php
+++ b/include/class.file.php
@@ -172,10 +172,18 @@ class AttachmentFile {
             .',type='.db_input($file['type'])
             .',size='.db_input($file['size'])
             .',name='.db_input($file['name'])
-            .',hash='.db_input($file['hash'])
-            .',filedata='.db_input($file['data']);
+            .',hash='.db_input($file['hash']);
 
-        return db_query($sql)?db_insert_id():0;
+        if (!(db_query($sql) && ($id=db_insert_id())))
+            return false;
+
+        foreach (str_split($file['data'], 1024*100) as $chunk) {
+            if (!db_query('UPDATE '.FILE_TABLE.' SET filedata = CONCAT(filedata,'
+                    .db_input($chunk).') WHERE id='.db_input($id)))
+                # Remove partially uploaded file contents
+                return false;
+        }
+        return $id;
     }
 
     /* Static functions */
-- 
GitLab