From: =?UTF-8?B?RHXFoWFuIE5vdmFrb3ZpxIc=?= on
hello,

I have some problems with storing files in db and retriving them, so
probably I'm doing something wrong :-)
Here is the case:
I have on one of the pages request to generate some PDF files and
store them in database. So, I use FPDF to create files, and that's
working perfect. Then my system is collecting generated file(s) and
storing them in DB. On the other side (page) I have to show stored
files from database.

So, when file (in this case PDF) is created and stored in some dir
(ex. tmp/file.pdf), by using function: fopen(filename, 'r'); $file =
fread(....); I put file(s) in array $tmpArray = array('file' =>
base64_encode($file).... ) and send it to model (db). There I have
table for files, (column type for file is BLOB). So, first I do the
$fileThatWillBeStored = base64_decode($file); and than store it. After
that I'm able to see that in DB there is one row with file, so that
part is also ok.
On the other side when I have to show that file, I just fetch it from
DB and again pack it in array, but first do the
base64_encode($fileFromDB) and send it to controller, where after
doing $file = base64_decode($fileFromDB); I just show it like this:
header("Content-length: ".$file['file_size']);
header("Content-type: ".$file['file_type']);
header("Content-Disposition: attachment; filename= ".$file['file_name']);
echo $file['file'];

And final result is something like:

%PDF-1.3
3 0 obj
<</Type /Page
/Parent 1 0 R
/Resources 2 0 R
/Contents 4 0 R>>
endobj
4 0 obj
<</Filter /FlateDecode /Length 1214>>
stream
x��W�v�F �� �L ��[�� N ��`'
�,{�� $�H�g� �/x�R?$� �Y� ��n=�U0t9"X ��h �O�)� ��5��� � � �Q:��2B!U��( I)0

......


So, I hope you get the picture :-) Of course, I've skiped lot of
steps, because the code is huge.

Any suggestions? additional questions?

P.S.
I can't read on the other side file from directory, so it has to be in
the way I just described ( Generate PDF with FPDF => pack in array,
but first base64_encode => send it to model => get from array and
unpack it with base64_decode on DB side => store in DB, and then
reverse: get from DB => pack in array, but first base64_encode => send
it to controller => get it from array and unpack with base 64_decode
=> show in view with headers)

Thnx,
Dusan


--
mob: + 46 70 044 9432
web: http://novakovicdusan.com

Please consider the environment before printing this email.
From: Nilesh Govindarajan on
Have you checked the headers? Its mostly a mime time issue I feel.
What do you see in Content-Type? It should be application/pdf (or
application/x-pdf, unsure) for PDF files.

--
Regards,
Nilesh Govindarajan
Facebook: http://www.facebook.com/nilesh.gr
Twitter: http://twitter.com/nileshgr
Website: http://www.itech7.com
VPS Hosting: http://www.itech7.com/a/vps
From: =?UTF-8?B?RHXFoWFuIE5vdmFrb3ZpxIc=?= on
Hello,

so when I'm sending the array to model it's like this:

$fp = fopen(INVOICE_PATH.date('Y-m-d').DS.$pdfName, "r");
$pdfContent = array(
'file' => base64_encode(fread($fp,
filesize(INVOICE_PATH.date('Y-m-d').DS.$pdfName))),
'name' => $pdfName,
'size' => filesize(INVOICE_PATH.date('Y-m-d').DS.$pdfName),
'type' => mime_content_type(INVOICE_PATH.date('Y-m-d').DS.$pdfName)
);
fclose($fp);

so the data in db are ok, and this type is application/pdf.

And when I'm getting data, I get the array like this:

$file = Array
(
[id] => 2
[file] =>VBERi0xLjM... <= here file is base64_encode()
[file_size] => 2204
[file_type] => application/pdf
[file_name] => 2_file.pdf
)

Headers:

header("Content-length: ".$file['file_size']);
header("Content-type: ".$file['file_type']);
header("Content-Disposition: attachment; filename= ".$file['file_name']);
echo base64_decode($file['file']);


So, mime looks ok, but still... not working :-(

2010/7/27 Nilesh Govindarajan <lists(a)itech7.com>:
> Have you checked the headers? Its mostly a mime time issue I feel.
> What do you see in Content-Type? It should be application/pdf (or
> application/x-pdf, unsure) for PDF files.
>
> --
> Regards,
> Nilesh Govindarajan
> Facebook: http://www.facebook.com/nilesh.gr
> Twitter: http://twitter.com/nileshgr
> Website: http://www.itech7.com
> VPS Hosting: http://www.itech7.com/a/vps
>



--
mob: + 46 70 044 9432
web: http://novakovicdusan.com

Please consider the environment before printing this email.
From: Peter Lind on
2010/7/27 Dušan Novaković <ndusan(a)gmail.com>:
> Hello,
>
> so when I'm sending the array to model it's like this:
>
> $fp = fopen(INVOICE_PATH.date('Y-m-d').DS.$pdfName, "r");
> $pdfContent             = array(
>                                        'file'  =>      base64_encode(fread($fp,
> filesize(INVOICE_PATH.date('Y-m-d').DS.$pdfName))),
>                                        'name'  =>      $pdfName,
>                                        'size'  =>      filesize(INVOICE_PATH.date('Y-m-d').DS.$pdfName),
>                                        'type'  =>      mime_content_type(INVOICE_PATH.date('Y-m-d')..DS.$pdfName)
>                                        );
> fclose($fp);
>
> so the data in db are ok, and this type is application/pdf.
>
> And when I'm getting data, I get the array like this:
>
> $file = Array
> (
>    [id] => 2
>    [file] =>VBERi0xLjM... <= here file is base64_encode()
>    [file_size] => 2204
>    [file_type] => application/pdf
>    [file_name] => 2_file.pdf
> )
>
> Headers:
>
> header("Content-length: ".$file['file_size']);
> header("Content-type: ".$file['file_type']);
> header("Content-Disposition: attachment; filename= ".$file['file_name']);
> echo base64_decode($file['file']);
>
>
> So, mime looks ok, but still... not working :-(
>
> 2010/7/27 Nilesh Govindarajan <lists(a)itech7.com>:
>> Have you checked the headers? Its mostly a mime time issue I feel.
>> What do you see in Content-Type? It should be application/pdf (or
>> application/x-pdf, unsure) for PDF files.
>>
>> --
>> Regards,
>> Nilesh Govindarajan
>> Facebook: http://www.facebook.com/nilesh.gr
>> Twitter: http://twitter.com/nileshgr
>> Website: http://www.itech7.com
>> VPS Hosting: http://www.itech7.com/a/vps
>>
>
>
>
> --
> mob: + 46 70 044 9432
> web: http://novakovicdusan.com
>
> Please consider the environment before printing this email.
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

Why are you storing files in the db in the first place? If they're
files why not use the filesystem?

Regards
Peter

--
<hype>
WWW: http://plphp.dk / http://plind.dk
LinkedIn: http://www.linkedin.com/in/plind
BeWelcome/Couchsurfing: Fake51
Twitter: http://twitter.com/kafe15
</hype>
From: =?UTF-8?B?RHXFoWFuIE5vdmFrb3ZpxIc=?= on
Hello Peter,

I can't use files from filesystem. Let's say that they are not on the
some mashine where is application that has to show files :-) So it has
to be in exact order as I wrote :-(

2010/7/27 Peter Lind <peter.e.lind(a)gmail.com>:
> 2010/7/27 Dušan Novaković <ndusan(a)gmail.com>:
>> Hello,
>>
>> so when I'm sending the array to model it's like this:
>>
>> $fp = fopen(INVOICE_PATH.date('Y-m-d').DS.$pdfName, "r");
>> $pdfContent             = array(
>>                                        'file'  =>      base64_encode(fread($fp,
>> filesize(INVOICE_PATH.date('Y-m-d').DS.$pdfName))),
>>                                        'name'  =>      $pdfName,
>>                                        'size'  =>      filesize(INVOICE_PATH.date('Y-m-d').DS.$pdfName),
>>                                        'type'  =>      mime_content_type(INVOICE_PATH.date('Y-m-d').DS.$pdfName)
>>                                        );
>> fclose($fp);
>>
>> so the data in db are ok, and this type is application/pdf.
>>
>> And when I'm getting data, I get the array like this:
>>
>> $file = Array
>> (
>>    [id] => 2
>>    [file] =>VBERi0xLjM... <= here file is base64_encode()
>>    [file_size] => 2204
>>    [file_type] => application/pdf
>>    [file_name] => 2_file.pdf
>> )
>>
>> Headers:
>>
>> header("Content-length: ".$file['file_size']);
>> header("Content-type: ".$file['file_type']);
>> header("Content-Disposition: attachment; filename= ".$file['file_name']);
>> echo base64_decode($file['file']);
>>
>>
>> So, mime looks ok, but still... not working :-(
>>
>> 2010/7/27 Nilesh Govindarajan <lists(a)itech7.com>:
>>> Have you checked the headers? Its mostly a mime time issue I feel.
>>> What do you see in Content-Type? It should be application/pdf (or
>>> application/x-pdf, unsure) for PDF files.
>>>
>>> --
>>> Regards,
>>> Nilesh Govindarajan
>>> Facebook: http://www.facebook.com/nilesh.gr
>>> Twitter: http://twitter.com/nileshgr
>>> Website: http://www.itech7.com
>>> VPS Hosting: http://www.itech7.com/a/vps
>>>
>>
>>
>>
>> --
>> mob: + 46 70 044 9432
>> web: http://novakovicdusan.com
>>
>> Please consider the environment before printing this email.
>>
>> --
>> PHP General Mailing List (http://www.php.net/)
>> To unsubscribe, visit: http://www.php.net/unsub.php
>>
>>
>
> Why are you storing files in the db in the first place? If they're
> files why not use the filesystem?
>
> Regards
> Peter
>
> --
> <hype>
> WWW: http://plphp.dk / http://plind.dk
> LinkedIn: http://www.linkedin.com/in/plind
> BeWelcome/Couchsurfing: Fake51
> Twitter: http://twitter.com/kafe15
> </hype>
>



--
mob: + 46 70 044 9432
web: http://novakovicdusan.com

Please consider the environment before printing this email.