From: Peter Lind on
2010/7/27 Dušan Novaković <ndusan(a)gmail.com>:
> 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 :-(
>

That normally doesn't stop people (mounting filesystems across the
network comes to mind) but if you say you need it I won't question it
further :)

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: Nilesh Govindarajan 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 :-(
>
>
>
>
> --
> mob: + 46 70 044 9432
> web: http://novakovicdusan.com
>
> Please consider the environment before printing this email.
>

Are you sure that you need Content-Disposition? Try removing that. I
think that's used only in emails (Correct me if I'm wrong).

--
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: Peter Lind on
2010/7/27 Nilesh Govindarajan <lists(a)itech7.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 :-(
>>
>>
>>
>>
>> --
>> mob: + 46 70 044 9432
>> web: http://novakovicdusan.com
>>
>> Please consider the environment before printing this email.
>>
>
> Are you sure that you need Content-Disposition? Try removing that. I
> think that's used only in emails (Correct me if I'm wrong).

You're wrong. Content-Disposition tells the browser how to handle the
content - in this case, the browser will download the file instead of
displaying it.

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: Richard Quadling on
2010/7/27 Dušan Novaković <ndusan(a)gmail.com>:
> 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.
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

As soon as you've got the data, create a hash of the file (sha1(),
md5(), etc.) and store that in the database.

When you restore the file, generate a new hash and compare it with the
stored one to make sure you've got the right data.

Assuming that the hashes match, then the next thing is to use a tool
like FireBug to see EXACTLY what is being received by the client. This
seems to be different to what you are sending.

The headers I use for a PDF file download (as compared to a PDF online
view) are ...

header("{$_SERVER['SERVER_PROTOCOL']} 200 OK", True, 200);
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Content-Length: ' . filesize($a_FPN['PDF']));
header('Content-Type: application/pdf');
header('Expires: 0');
header('Pragma: public');

// Force a Save Dialogue if that is required.
if ('True' === $b_Save)
{
header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename="Jobsheet ' .
$s_OrderNumber . '.pdf"');
header('Content-Transfer-Encoding: binary');
}

readfile($a_FPN['PDF']);
From: Pete Ford on
On 27/07/10 14:16, Peter Lind wrote:
> 2010/7/27 Nilesh Govindarajan<lists(a)itech7.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 :-(
>>>
>>>
>>>
>>>
>>> --
>>> mob: + 46 70 044 9432
>>> web: http://novakovicdusan.com
>>>
>>> Please consider the environment before printing this email.
>>>
>>
>> Are you sure that you need Content-Disposition? Try removing that. I
>> think that's used only in emails (Correct me if I'm wrong).
>
> You're wrong. Content-Disposition tells the browser how to handle the
> content - in this case, the browser will download the file instead of
> displaying it.
>
> Regards
> Peter
>

I think you need to be careful about quoting the file name in the
Content-Disposition header: something like

header('Content-Disposition: attachment; filename="'.$filename.'.xml"');

seems to be the right quoting - the filename needs to be in double-quotes

--
Peter Ford, Developer phone: 01580 893333 fax: 01580 893399
Justcroft International Ltd. www.justcroft.com
Justcroft House, High Street, Staplehurst, Kent TN12 0AH United Kingdom
Registered in England and Wales: 2297906
Registered office: Stag Gates House, 63/64 The Avenue, Southampton SO17 1XS