Re: storing files in database and retriving them

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



2010/7/27 Dušan Novaković <ndusan@xxxxxxxxx>:
> 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']);

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux