title: Better way to store large files in a MySQL database?
body: <p>I have a PHP script that you can upload very large files with (up to 500MB), and the file's content is stored in a MySQL database. Currently I do something like this:</p>
  
  <pre><code>mysql_query("INSERT INTO table VALUES('')");
  
  $uploadedfile = fopen($_FILES['file']['tmp_name'], 'rb');
  while (!feof($uploadedfile)) {
      $line = mysql_escape_string(fgets($uploadedfile, 4096));
      mysql_query("UPDATE table SET file = CONCAT(file, '$line') WHERE something = something");
  }
  fclose($uploadedfile);
  </code></pre>
  
  <p>This of course does a bloody lot of sql queries.</p>
  
  <p>I did that rather than something like</p>
  
  <pre><code>$file = file_get_contents($_FILES['file']['tmp_name']);
  mysql_query("INSERT INTO table VALUES('$file')");
  </code></pre>
  
  <p>because that would use up however much memory the file was, and it seemed better to do more sql queries than to use 500 mb of memory.<br>
  However, there must be a better way. Should I go ahead and do it the file_get_contents way or is there a better way than CONCAT, or is the way I'm doing it now the lesser of all evils?</p>