title: Downloading Blob from MySQL Database to its original format with PHP
body: <p>I'm trying create a website where you can upload a file and afterwards you can download it to your local computer. I'm doing this by storing the the file in a blob and also storing the size of the file and the mime type of the file as well, now this is working as expected.</p>
  
  <p>What I want to do now is to download that blob of data and restore it to its original format. For example if I uploaded a <i>test.docx</i> file in the database I can download <i>test.docx</i> as it was before.</p>
  
  <p>I am managing to make it download but the problem I'm having is that it's downloading the HTML in the page and the blob is a bin (A lot of symbols).</p>
  
  <p><strong>Download.php</strong></p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
      &lt;title&gt;Download File From MySQL&lt;/title&gt;
      &lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
  &lt;?php
  $dbUser = "root";
  $dbPassword = "root";
  $dsn ='mysql:host=localhost;dbname=filerepository';
  
  $dbh = new PDO($dsn,$dbUser,$dbPassword);
  
  $query = "SELECT ID,Title FROM FoldersFiles";
  foreach($dbh-&gt;query($query,PDO::FETCH_ASSOC) as $row)
  {
  ?&gt;
          &lt;a href="download.php?id=&lt;?php echo $row['ID'];?&gt;"&gt;&lt;?php echo $row['Title'];?&gt;&lt;/a&gt; &lt;br&gt;
  &lt;?php
  }
  
  if(isset($_GET['id']))
  {
      $query2 = "SELECT Title,Format,Content,FileSize FROM  FoldersFiles WHERE ID = ?";
      $sth = $dbh-&gt;prepare($query2);
      $sth-&gt;execute(array($_GET['id']));
      $result =  $sth-&gt;fetchAll(PDO::FETCH_ASSOC);
      foreach($result as $row)
      {
          $size = $row['FileSize'];
          $type = $row['Format'];
          $title  = $row['Title'];
          $content = $row['Content'];
      }
      require_once('/var/www/uploadFile/downloadFile.php');
      Download($size,$type,$title,$content);
  }
  
  ?&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><strong>downloadFile.php</strong></p>
  
  <pre><code>&lt;?php
  function Download($size,$type,$title,$content)
  {
  
          header("Content-Length: $size");
          header("Content-Type: $type");
          header("Content-Disposition: attachment; filename=\"$title\"");
  
          echo $content;
  
  }
  </code></pre>
  
  <p>I have split them because there was a similar problem where he fixed it by doing that but I still am having the problem.</p>
  
  <p>I am using Ubuntu as my OS to develop this website, might this affect it? Also when I download the file it doesn't download in any format.</p>
  
  <p><strong>Data being downloaded</strong></p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
      &lt;title&gt;Download File From MySQL&lt;/title&gt;
      &lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
          &lt;a href="download.php?id=46"&gt;Main&lt;/a&gt; &lt;br&gt;
          &lt;a href="download.php?id=65"&gt;ICE&lt;/a&gt; &lt;br&gt;
          &lt;a href="download.php?id=66"&gt;PHP&lt;/a&gt; &lt;br&gt;
          &lt;a href="download.php?id=67"&gt;National Diploma Year 2&lt;/a&gt; &lt;br&gt;
          &lt;a href="download.php?id=68"&gt;National Diploma Year 1&lt;/a&gt; &lt;br&gt;
          &lt;a href="download.php?id=73"&gt;Semester 1&lt;/a&gt; &lt;br&gt;
          &lt;a href="download.php?id=74"&gt;Object Orient Programing&lt;/a&gt; &lt;br&gt;
          &lt;a href="download.php?id=75"&gt;Presentations&lt;/a&gt; &lt;br&gt;
          &lt;a href="download.php?id=76"&gt;Homework&lt;/a&gt; &lt;br&gt;
          &lt;a href="download.php?id=106"&gt;test&lt;/a&gt; &lt;br&gt;
          &lt;a href="download.php?id=108"&gt;test&lt;/a&gt; &lt;br&gt;
          &lt;a href="download.php?id=109"&gt;Main&lt;/a&gt; &lt;br&gt;
          &lt;a href="download.php?id=111"&gt;File Repository&lt;/a&gt; &lt;br&gt;
          &lt;a href="download.php?id=114"&gt;Documentation&lt;/a&gt; &lt;br&gt;
          &lt;a href="download.php?id=115"&gt;Documentation&lt;/a&gt; &lt;br&gt;
          &lt;a href="download.php?id=125"&gt;test&lt;/a&gt; &lt;br&gt;
  PK\0\0\0\0\0!\02‘oWf\0\0¥\0\0\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0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0¬“ÍjÃ0„ï…¾ƒØ{-;mC   ‘s)\\[÷d{ýCõc¤MZ¿}EJ‡ÓƒŽ3bg¾…Õzó­; ó½5²$†¦²uoZÅöá˜\'ij©¬A#zØä÷wë7T’ÂïúÁ³b¼€ŽhXqî«µô‰Ð„—Æ:-)H×òAVŸ²E¾HÓ%wÓÈ¯2Ù®àvõ#°bð?Ù¶iú
  _mµ×hèF÷H6ó!SºIÀÉIBðÛ‹¨4*œõ\\}³Þìu‰.l|!8[sË˜fñp”¿f6Çð“¡±†
  Yª  ÇÙšƒxŠ  ñ…åûŸ“œ˜\'~õÛò\0\0\0ÿÿ\0PK\0\0\0\0\0!\0K”¸ÄÅ0\0\0ž4\0\0\0\0word/document.xmlì}ëŽã¶¶æÿæ„š?`\'-RÔ­æ$gtMé=éN‚ó+PÙ¬²vÛ’¤êJå×~Œ3Àpžå&lt;Ê~’!)Ë%«lëbÙ–å•½Ñ*S²Lr}ëÊÅÅù×?séMÒ0Ž¾½AßÈ7&amp;ñ4Œ¾½ùõ“ÿµq#¥YMƒyÑoožizó¯ßý÷ÿö/O·Óxò¸ Q&amp;±WDéíÓròíÍ,Ë–·oß¦“]é7‹p’Äi|Ÿ}3‰oãûûpBß&gt;ÅÉô-–‘,þZ&amp;ñ„¦)û=\'ˆ¾éÍêu‹×o‹—4b7ïãddìcòðv$Ÿ—_³·/ƒ,¼çaöÌÞ-kÅkâoo“èvõŠ¯×â_¹Í;´ºßHšünþw5âß&amp;tÎúGé,\\¾£ëÛØÍYñ’/ûñe1/ž{Z\"r
  Ü$xb——6éþ4ÿÒbž÷|ÿ‘Ü€\"üëo4éÂæo=YaôòÃ¦¦4¹Hm÷\\}Áòá0â|ŸÄË—·…‡½í]ôyý.ÎÙ-Þµ\"ryhéaù8–Œ“ÛwQœwsÖ#F2‰ÍºÄa}ó“8wñô™_—¬™Ü.ƒ$x7ýöF³LKÖ-óF´fôÏŒ·ê«ÿXë-“nÓ_¾½‘eOG²j­›&gt;$¼ÑT0BÚºÑ¥÷Áã&lt;{ýø‡R“èÅ‡„_Òe0a#`÷åoä_˜‡|N1Yøå‘)xÌâ›·ükŸ°_‚ù·7Æó4É[“ü‰GYÊ_™NBFçô!¦Ò¯ïøËhfVßÞ|
  4•~¦OÒ/ñ\"ˆøÍ™¥•ç\'ié³ø‘ô¯â§1)Zþk¥¶·«¾¼]3Ù:k\'ëô]þ¯“Šë$žÇ¼C¢Ç²ø¯:6E{=¶¼­ÛÓ­Ð]‚‚Œ6Ë„¦4ùBo¾ói=²O4Ë¿OÅNôq”œ}Ñã\"ÿ#œ™¯\'$9»÷nZ´¡ÕÈ×_èˆ[&gt;Bk&gt;DBõ¯^~¤”?|B7@\0V¶ [´
  Ý½Œ†cõ×å&lt;¦Íqkn·À­?Eíë\0r¹@î‡$üÎéMß| éÓ[:
  ³¯šÃØŒ\0cæ%ŒªÃÄü·pÞÃ`0|~owæœxÁ©•Jq$e3*ÝÇó)MR)ˆ¦RÂlãéù\0v\0û,æ ¤y¸³€G9¼i0™I÷ß4†³ÍÉp8ŸÎ&gt;³¡¥e/i’…Ì–¾çŸÓð/ÚÜ±`°&lt;,\'ôKÈWUSéÍ$^2HKñ½ÄlI {µäš¶@7îÝ²G,Ïtº›£{»•ý&gt;~xà¤Þ€siÌuà¼&lt;&amp;¾ge®²„    –¶0»”þ™T0s?Ìü+»J¿Ð‡0Írd7wmÀ`Åû)ˆ‚*’ªò°ôñ9Íè¢9œU€3Ày¨rZ`&lt;}¼K\'I¸l)§94Ø\0ì³¯;Ñ ™Ì]›C—ƒô‚2ýNB.9}{\'Ùè’EÖœºhÃ\'¹o¾4n]†“L \"ˆ&amp;³8Y£ÅÐtFÂ/·    e÷CÖúÇŸü\'þ‘ŒU¾àY¤ó†Ólv+ÿÏ
  fÙ-úF]f7R|;K.ž×‰°¢)ÍØkV·ÅÌÕž‹a|{ó?™ÿ¿8‰?SöàýªŸ¼ƒDG!ˆ#‹&gt;4É‘Ý®Ówù¿\'ÉÈæ‰‹ôöàÓZñjØ`ÝÚý¥cóHÚ$ˆ¤IBƒŒò”0‘â§Hzà›yšzðÙ®\0è«\0´W¤˜HOÌ “fÁZúk¦ÜB~KO3š°6*@ÜŸ†÷÷¬6XÄì2¥KñÍ¦&lt;}e?I‹Gö~ö•eðÜf9eðY´ÀWÁ&lt;\"—Jƒ}šÅ rXÇÍ9†g2,‘Îøƒ¬eÁX(›IK/ç´êw&gt;z˜—†ÁÝ&gt;ºq³ËG×é£Ÿ+Ñöš|t‡|tðÑ‹ÖCvMÝ×X{9ƒÏÖ,$+ØlC„vo6Û»{é9~\\YcÁjˆ0Ÿ&amp;¾—B6\0žå•ïIïÖÛG¸Ç¿2•8¥ANØwž›ãÿùœ€À\'ŸEòE1‰ ó,aÏ%¸g¯§æ“ÿ%NKÍŸ¥GQe€1Ë„&amp;YFy\"d^G
  Ëû¬š{3ÎàS\"®Jäy5wÌC…¿Rüžˆx=ÐLŠâ,¼™ï™‚x–&amp;³ z`Ýg_ 3¡2¸a¯Zo;œË%Z°Åà*-®‚-~PŸB7`¬±Þ-.±ùûcÊ0/-ã%S&lt;ú   f™ÇL\'´@üàs.ñWø)]2&lt;
  </code></pre>
  
  <p>Please tell me if you need any more information that might help you :D</p>