Re: storing files in database and retriving them

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

 



Ok,

I found the problem :-) There was nothing wrong with storing or
sending files, it's just that I tried to load file with jQuery on
wrong way :-(

Sorry once more to everyone :-(

Dusan

2010/7/28 Dušan Novaković <ndusan@xxxxxxxxx>:
> Hi,
>
> Here are headers:
>
> Response Headers
> ----------------------------------------------------------------------------------------------------
>
> Date                                Wed, 28 Jul 2010 06:41:33 GMT
> Server                      Apache/2.2.14 (Ubuntu)
> X-Powered-By                PHP/5.3.2-1ubuntu4.1
> Expires                     0
> Cache-Control               must-revalidate, post-check=0, pre-check=0
> Pragma                      no-cache
> Content-Length              1849
> Accept-Ranges               bytes
> Content-Disposition         attachment; filename='10_file.pdf'
> Content-Transfer-Encoding       binary
> Content-Encoding            gzip
> Vary                                Accept-Encoding
> Content-Type               application/pdf
> X-Cache                    ....
> X-Cache-Lookup             ....
> Via                                ....
> Connection                 keep-alive
> Proxy-Connection           keep-alive
>
>
> Request Headers
> ----------------------------------------------------------------------------------------------------
>
> Host                               .......
> User-Agent                 Mozilla/5.0 (X11; U; Linux i686; sv-SE;
> rv:1.9.2.3) Gecko/20100423 Ubuntu/10.04 (lucid) Firefox/3.6.3
> Accept                     text/html, */*
> Accept-Language    sv-se,sv;q=0.8,en-us;q=0.5,en;q=0.3
> Accept-Encoding    gzip,deflate
> Accept-Charset             ISO-8859-1,utf-8;q=0.7,*;q=0.7
> Keep-Alive                         115
> Proxy-Connection           keep-alive
> Content-Type               application/x-www-form-urlencoded; charset=UTF-8
> X-Requested-With           XMLHttpRequest
> Referer                    http://........../user/queue/
> Content-Length             5
> Cookie                     PHPSESSID=67bf56153fb3b9c69ed214114c8154dd
>
>
>
>
> Dusan
>
>
> 2010/7/27 Richard Quadling <rquadling@xxxxxxxxx>:
>> 2010/7/27 Dušan Novaković <ndusan@xxxxxxxxx>:
>>> I don't think that it will help :-( I've tried to set different
>>> headers but still I end up with some strange response, 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
>>> ....
>>>
>>> So, if we try to look into this problem as basic. For example, I'm
>>> trying to store file test.pdf from some location on server into my db
>>> like this:
>>>
>>> $fp = fopen(PATH_TO_PDF_FILE.'test.pdf', "r");
>>> $size = filesize(PATH_TO_PDF_FILE.'test.pdf');
>>> $type = mime_content_type(PATH_TO_PDF_FILE.'test.pdf');
>>>
>>> $file = fread($fp, $size);
>>>
>>> Here if I do : var_dump($file) how the content should look like?
>>>
>>> $file = base64_encode($file);
>>> close($fp);
>>>
>>> $query = sprintf("INSERT INTO `table` SET `file`='%s', `name`='%s',
>>> `size`='%s', `type`='%s'",
>>>                        mysql_real_escape_string($file),
>>>                        mysql_real_escape_string('test.pdf'),
>>>                        mysql_real_escape_string($size),
>>>                        mysql_real_escape_string($type)
>>> );
>>> mysql_query($query);
>>>
>>>
>>>
>>> And lets try to retrive data now and show them on page:
>>>
>>> $query  = sprintf("SELECT * FROM `table` WHERE `id`='something'");
>>> $res= mysql_query($query);
>>> $row = mysql_fetch_assoc($res);
>>>
>>>  header("{$_SERVER['SERVER_PROTOCOL']} 200 OK", True, 200);
>>>  header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
>>>  header('Content-Length: ' . $row['size']);
>>>  header('Content-Type: ' . $row['type']);
>>>  header('Expires: 0');
>>>  header('Pragma: public');
>>>
>>> header('Content-Description: File Transfer');
>>> header('Content-Disposition: attachment; filename=" . $row['name']);
>>> header('Content-Transfer-Encoding: binary');
>>>
>>>  readfile(base64_decode($row['file']));
>>>
>>>
>>> And here I should get PDF file, but instead I get the same symbols on
>>> the top of page :-(
>>>
>>> Any suggestion? Some different header or ... ?
>>>
>>>
>>> 2010/7/27 Richard Quadling <rquadling@xxxxxxxxx>:
>>>> 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']);
>>>>
>>>
>>>
>>>
>>> --
>>> mob: + 46 70 044 9432
>>> web: http://novakovicdusan.com
>>>
>>> Please consider the environment before printing this email.
>>>
>>
>> Like I said ... if you don't think there is a problem with what you
>> are SENDING, take a look at the browser and see what it is
>> RECEIVING...
>>
>> For that, any of the good browsers will allow you to see the headers
>> of a request. What headers are you RECEIVING.
>>
>> Chrome and FireFox/FireBug would be the tools I'd be looking at next.
>>
>> If not, then WireShark - but that would be overkill.
>>
>> Anything in the middle (caching, proxy server, firewalls, A/V s/w)
>> COULD be changing the header. Probably not, but at least this way you
>> could report back EXACTLY what the client is receiving.
>>
>
>
>
> --
> mob: + 46 70 044 9432
> web: http://novakovicdusan.com
>
> Please consider the environment before printing this email.
>



-- 
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




[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