# SQL Blob file

**URL:** <https://www.phphelp.com/t/sql-blob-file/10380>\
**Category:** Beginners - Learning PHP\
**Created:** [September 25, 2007, 6:18pm UTC](https://www.phphelp.com/t/sql-blob-file/10380 "2007-09-25T18:18:25Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![andyd34](https://www.phphelp.com/letter_avatar_proxy/v4/letter/a/46a35a/32.png) [@andyd34](https://www.phphelp.com/u/andyd34)\
**Post date:** [September 25, 2007, 6:18pm UTC](https://www.phphelp.com/t/sql-blob-file/10380/1 "2007-09-25T18:18:25Z")

</div>

I have files stored in a mysql as blob’s how would i retrieve them as downloads.

Anyone have a suggestion

Andy

---

<div class="post-metadata">

**Author:** ![Q1712](https://www.phphelp.com/letter_avatar_proxy/v4/letter/q/a183cd/32.png) [@Q1712](https://www.phphelp.com/u/Q1712)\
**Post date:** [September 25, 2007, 6:22pm UTC](https://www.phphelp.com/t/sql-blob-file/10380/2 "2007-09-25T18:22:02Z")

</div>

just put the right header.

e.g.  
header(‘Content-Type: image/png’);  
header(‘Content-Disposition: attachment; filename=“whatever.png”’);

and then put out the content of the field with echo.

---

<div class="post-metadata">

**Author:** ![andyd34](https://www.phphelp.com/letter_avatar_proxy/v4/letter/a/46a35a/32.png) [@andyd34](https://www.phphelp.com/u/andyd34)\
**Post date:** [September 25, 2007, 7:49pm UTC](https://www.phphelp.com/t/sql-blob-file/10380/3 "2007-09-25T19:49:42Z")

</div>

I have been onto php net and had a look under headers as you suggest above. I came across this and have played about with it for a bit but I am not getting an output

[php]

\<?php $file = $row['name']; while (strpos($file)==false) { $file = substr($file); // cut the first char. } header ('Content-type: application/'.$file); header('Content-Disposition: attachment; filename="'.$file.'"'); $tmp = file\_get\_contents($row['name']); echo $tmp; ?\> 

[/php]

I’m not sure whether its the file type that is causing the problem. The extension is .mxp

---

<div class="post-metadata">

**Author:** ![Q1712](https://www.phphelp.com/letter_avatar_proxy/v4/letter/q/a183cd/32.png) [@Q1712](https://www.phphelp.com/u/Q1712)\
**Post date:** [September 25, 2007, 8:07pm UTC](https://www.phphelp.com/t/sql-blob-file/10380/4 "2007-09-25T20:07:07Z")

</div>

i thougt the file itself is in the database, isn’t it?

what errors are u getting with error\_reporting(E\_ALL) ?

substr and strpos need 2 parameter!

---

<div class="post-metadata">

**Author:** ![andyd34](https://www.phphelp.com/letter_avatar_proxy/v4/letter/a/46a35a/32.png) [@andyd34](https://www.phphelp.com/u/andyd34)\
**Post date:** [September 25, 2007, 8:10pm UTC](https://www.phphelp.com/t/sql-blob-file/10380/5 "2007-09-25T20:10:55Z")

</div>

the file itself is in the database. I am not getting any errors

and the $file string is echoing ok so am not losing the contents.

Its just not doing anything apart from loading hte page

---

<div class="post-metadata">

**Author:** ![andyd34](https://www.phphelp.com/letter_avatar_proxy/v4/letter/a/46a35a/32.png) [@andyd34](https://www.phphelp.com/u/andyd34)\
**Post date:** [September 25, 2007, 8:14pm UTC](https://www.phphelp.com/t/sql-blob-file/10380/6 "2007-09-25T20:14:59Z")

</div>

heres what i have now.

[php]  
$file = $row[‘content’];  
header(“Content-type:application/x-mmxp”);// It will be called downloaded.pdf  
header(“Content-Disposition:attachment;filename=’”.$file."’");// The PDF source is in original.pdf  
readfile($file);  
[/php]

The $row[‘content’] is the blob file on the database is that right.

---

<div class="post-metadata">

**Author:** ![Q1712](https://www.phphelp.com/letter_avatar_proxy/v4/letter/q/a183cd/32.png) [@Q1712](https://www.phphelp.com/u/Q1712)\
**Post date:** [September 25, 2007, 8:37pm UTC](https://www.phphelp.com/t/sql-blob-file/10380/7 "2007-09-25T20:37:34Z")

</div>

i have no idea how ur db is looking like.

i guess u’ll need something like:  
[php]header(“Content-type: application/x-mmxp”);  
header(‘Content-Disposition: attachment; filename="’.addslashes($row[‘name’]).’"’);  
echo $row[‘content’];[/php]

---

<div class="post-metadata">

**Author:** ![andyd34](https://www.phphelp.com/letter_avatar_proxy/v4/letter/a/46a35a/32.png) [@andyd34](https://www.phphelp.com/u/andyd34)\
**Post date:** [September 25, 2007, 9:15pm UTC](https://www.phphelp.com/t/sql-blob-file/10380/8 "2007-09-25T21:15:22Z")

</div>

All that this does is echo the file

> mxi? ;IXMMmXmM?x?µS?j?@=;????c©??»?8?jpq??XE[?»??n?}gW??¤??r???7o??y®±d«©R8?'O?)k??&`P?"?|??V6O???#qD-???S~?J?? ?K??|T¶?2? ??M??#?E?9P???L?s ?±V?,V???!p???W??\_??????v] ?|K?\*??«?\_??N???x?u?±?0EgM??????;:?? I ?y¶h m?ZP?????v?=??J ?=???.·??«?x m§Lh ?8??»S¶?Lh?? 1??u¦?ye?§?B.?sY?¶~7·+?k??w?M??(¤?-???«]??¤?CXT?-i?9?"???\> B?PHP Page Counter.htm? \*$TXETrWmD?x?­?[k?0???§~ja¶???- t¶????2?OF?§¶?-?8?¶?\_??B?Z m?@??|??f??#????J?bN¶C???t2»??BqsX??-¤?©?k?|??rkM????E???P·?h??O????T?S??±?¦6???z ?8??\_G?+q??2?X???4??±Q\>?¤a??¤I8????;­¤???¦i?f???µ¬xl?[???36?.?I?????(?$???????+c?Z¶?e?\*P?5u{D \<????x!®??r~?)?y?µh??-?A?#???] \*j??=?????

I tried readfile but nothing happens

---

<div class="post-metadata">

**Author:** ![andyd34](https://www.phphelp.com/letter_avatar_proxy/v4/letter/a/46a35a/32.png) [@andyd34](https://www.phphelp.com/u/andyd34)\
**Post date:** [September 25, 2007, 9:20pm UTC](https://www.phphelp.com/t/sql-blob-file/10380/9 "2007-09-25T21:20:10Z")

</div>

The db is

`id` int(11) NOT NULL auto\_increment,  
`name` varchar(30) collate latin1\_general\_ci NOT NULL default ‘’,  
`type` varchar(30) collate latin1\_general\_ci NOT NULL default ‘’,  
`size` int(11) NOT NULL default ‘0’,  
`content` blob NOT NULL,  
PRIMARY KEY (`id`)

it was easier to do this ![:wink:]()

---

<div class="post-metadata">

**Author:** ![Q1712](https://www.phphelp.com/letter_avatar_proxy/v4/letter/q/a183cd/32.png) [@Q1712](https://www.phphelp.com/u/Q1712)\
**Post date:** [September 25, 2007, 11:07pm UTC](https://www.phphelp.com/t/sql-blob-file/10380/10 "2007-09-25T23:07:49Z")

</div>

error\_reporting

no output before header

---

<div class="post-metadata">

**Author:** ![andyd34](https://www.phphelp.com/letter_avatar_proxy/v4/letter/a/46a35a/32.png) [@andyd34](https://www.phphelp.com/u/andyd34)\
**Post date:** [September 25, 2007, 11:20pm UTC](https://www.phphelp.com/t/sql-blob-file/10380/11 "2007-09-25T23:20:14Z")

</div>

Now I am confused, I cant output anything before

[php]  
header(“Content-type: application/x-mmxp”);  
header(‘Content-Disposition: attachment; filename="’.addslashes($row[‘name’]).’"’);  
echo $row[‘content’];  
[/php]

so how can I have $row[‘name’] without

[php]

$query = “SELECT \* FROM extensions”;  
$dw\_ext = mysql\_query($query, $dbcon) or die(‘Error, query failed’);  
$row = mysql\_fetch\_assoc($dw\_ext);

[/php]

before it???

---

<div class="post-metadata">

**Author:** ![Q1712](https://www.phphelp.com/letter_avatar_proxy/v4/letter/q/a183cd/32.png) [@Q1712](https://www.phphelp.com/u/Q1712)\
**Post date:** [September 25, 2007, 11:29pm UTC](https://www.phphelp.com/t/sql-blob-file/10380/12 "2007-09-25T23:29:40Z")

</div>

of cause u cann use these lines before header, because thats not generating output to the browser.

output to the browser is generated by echo/print or by anything outside \<?php ?\> (even a whitespace character)

---

<div class="post-metadata">

**Author:** ![andyd34](https://www.phphelp.com/letter_avatar_proxy/v4/letter/a/46a35a/32.png) [@andyd34](https://www.phphelp.com/u/andyd34)\
**Post date:** [September 25, 2007, 11:52pm UTC](https://www.phphelp.com/t/sql-blob-file/10380/13 "2007-09-25T23:52:37Z")

</div>

Now I get it, Thanks for your help
