display csv in browser with charset utf-8, using php and headers - php

In a cloud-like web application I want to display files directly in browser, which works perfectly for .PDF and images using this code:
if (file_exists($serverfile)) {
header('Content-Type: ' . mime_content_type ($serverfile) . '; charset=utf-8');
header('Content-Disposition: inline; filename="' . $origname . '"');
header('content-Transfer-Encoding: binary');
header('Content-length: ' . filesize($serverfile));
header('Accept-Ranges: bytes');
readfile($serverfile);
//... more unrelated code
exit;
}
but I have scrumbled characters e.g. with German Umlauts, in case the file is a .CSV
I also set ini_set('default_charset', 'utf-8'); on top of the script.
What am I missing ?

Related

White screen when output buffer php on some smartphone only

I am writing a website using bootstrap 4, html 5 and php 7.
The website handle a folder with many pdf files inside.
In order to control the access to the pdf file, i use a php file that output the pdf wanted.
The folder of pdf is protected with a htaccess file.
Everything is working perfectly expect on some smartphones which display a white screen instead of the pdf file.
Same for the download option.
For debugging, I added logs at each steps,
In the normal situation, where i get the pdf, i got "-9-13-"
In the situation with the white screen, i got "-9-13-16", so something weird with the "exit"
Any ideas ?
if (file_exists($file))
{
//log_dbg('9-');
if(isset($_POST['download']))//download
{
//log_dbg('10-');
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="' . $chapter_file_name . '"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file));
readfile($file);
//log_dbg('11-');
exit;
//log_dbg('12-');
}
else //Stream
{
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $chapter_file_name . '"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file));
header('Accept-Ranges: bytes');
readfile($file);
//log_dbg('13-');
exit;
//log_dbg('14-');
}
}
else
{
//log_dbg('15-');
echo 'Fichier '.$file.' introuvable';
}
//log_dbg('16-');
Do you have any idea what can be the problem ?
You can try in real here :
http://laguildedesecrivains.fr/story.php -> click on "Chapitre 1"

Display a pdf with PHP not displayed correctly

I have some pdf files on a storage server and i want to show them to the users without showing them the real file path, and i want to do this with PHP.
I tryed this:
$file = 'https://storage.server_test.com/agc/catalogs/catalog1.pdf';
$filename = 'catalog.pdf';
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file));
header('Accept-Ranges: bytes');
#readfile($file);
But the pdf is not displayed at all and i get this error: This PDF document might not be displayed correctly.
What am i doing wrong?
Have you tried embedding it using html object tag?
<object data="https://storage.server_test.com/agc/catalogs/catalog1.pdf" type="application/pdf">
<embed src="https://storage.server_test.com/agc/catalogs/catalog1.pdf" type="application/pdf" />
</object>
Alternatively in php, try this header:
header('Content-Disposition: attachment; filename="' . $filename . '"');
Finally, if #readfile($file) isn't working, try:
echo #file_get_contents($file);
After trying this myself locally, I think I managed to get it working. Try this:
<?php
$remote_pdf = 'http://www.saronicferries.gr/content/pdfFiles/sample.pdf';
$remote_file_name = end(explode('/', $remote_pdf));
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="'. $remote_file_name .'"');
header('Content-Transfer-Encoding: binary');
readfile($remote_pdf);
?>
Output:
I think what was the problem is that filesize($file) was failing for remote file. After removing that and header('Accept-Ranges: bytes');, it works.

downloading pdf file using php and html link

i have written some code to select a row from a database and generate a link. the link is working ok but when i try to download the pdf i get this:
����)�^�z8��AQ�[��rr�=��73KJ��KR]�PD�H�����>3;,;~˾����ɫS����/ؤ���,������=??<8��3��L�����e�\I�aN�i.����A�.�����������|x�F�oN���1>MʙJ�#�Mz�'�N��?K��sx`D�5gژ�r&�N3��M�f����߱9<]R��,))�dj���D#k&O-�7V����M��d�I��HMN=��9��/�ubS���`189\S�����f�p_��T�T�&ӭ���E>�O�)eAœ
displaying on the page.
my code is:
<?php
$sql="SELECT * from table1 where invoice_number = '".$_GET["inv"]."' and sequence = '".$_GET["seq"]."' ";
$rs=mysql_query($sql,$conn) or die(mysql_error());
if(mysql_num_rows($rs) > 0)
{
$result=mysql_fetch_array($rs);
$file = 'http://www.domain.co.uk/invoices/'.$result["pdf"].'';
$filename = 'Custom file name for the.pdf'; /* Note: Always use .pdf at the end. */
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file));
header('Accept-Ranges: bytes');
#readfile($file);
}
?>
any ideas how i can make it download the file rather than try and display it in the browser. please note, its a shared hosting server so i cannot make many changes on the actual server itself
Change the following:
header('Content-Disposition: inline; filename="' . $filename . '"');
to
header('Content-Disposition: attachment; filename="' . $filename . '"');

PHP-unable to download a .pdf file from database , file content is stored in mysql database

i have written a code in php which will echo a pdf file.whenever i am trying to echo that pdf,the browser page is changing into gray colors and the loading icon at the bottom left side corner is appearing and after that it fails to show that pdf file.
what i can assure you that code upto fetching the data from databse is perfect.there is no error or mistake.after fetching the data i have used following headers to echo that file.i am not sure about these headers.
$mimetype = 'application/pdf';
$disposition = 'attachment';
header('Content-type: $mimetype');
header('Content-Disposition: inline; filename="$question"');
header('Content-Transfer-Encoding: binary');
header('Content-length: ' . strlen($question));
header('Accept-Ranges: bytes');
echo "$question";
NOTE: I have used .pdf extension in content-decomposition.but that was not fruitful to me.also used readfile() function and it was also not helpful to me. Can anyone tell me what's wrong there?
The main reason why page is changing into gray colors is that the browser is unable to detect content type correctly.
Try this:
header("Content-type: $mimetype");
header('Content-Disposition: inline; filename="'.$question.'"'); // Filename should be there, not the content
Instead of :
header('Content-type: $mimetype');
header('Content-Disposition: inline; filename="$question"');
It seems that you have invalid quotes, so the content type is not specified correctly.
EDIT
To clear out, let's assume that $question is the binary PDF content.
That is what your code should be:
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename=anything.pdf');
header('Content-Transfer-Encoding: binary');
echo $question;
ERRORS EXPLAINED
Let's discuss your original code and your errors.
$mimetype = 'application/pdf';
$disposition = 'attachment';
// First error: you have single quotes here. So output is 'Content-type: $mimetype' instead of the 'Content-type: application/pdf'
header('Content-type: $mimetype');
// Second error. Quotes again. Additionally, $question is CONTENT of your PDF, why is it here?
header('Content-Disposition: inline; filename="$question"');
header('Content-Transfer-Encoding: binary');
// Also bad: strlen() for binary content? What for?
header('Content-length: ' . strlen($question));
header('Accept-Ranges: bytes');
echo "$question";
ONE MORE EDIT
i have another query...i want to change the filename into $year.pdf..$
year may have values like 2007..how can i do that???
Try this :
$year = '2013'; // Assign value
header('Content-Disposition: inline; filename='.$year.'.pdf');
Instead of:
header('Content-Disposition: inline; filename=anything.pdf');

How to Automatically Start a Download in PHP?

What code do you need to add in PHP to automatically have the browser download a file to the local machine when a link is visited?
I am specifically thinking of functionality similar to that of download sites that prompt the user to save a file to disk once you click on the name of the software?
Send the following headers before outputting the file:
header("Content-Disposition: attachment; filename=\"" . basename($File) . "\"");
header("Content-Type: application/octet-stream");
header("Content-Length: " . filesize($File));
header("Connection: close");
#grom: Interesting about the 'application/octet-stream' MIME type. I wasn't aware of that, have always just used 'application/force-download' :)
Here is an example of sending back a pdf.
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="' . basename($filename) . '"');
header('Content-Transfer-Encoding: binary');
readfile($filename);
#Swish I didn't find application/force-download content type to do anything different (tested in IE and Firefox). Is there a reason for not sending back the actual MIME type?
Also in the PHP manual Hayley Watson posted:
If you wish to force a file to be downloaded and saved, instead of being rendered, remember that there is no such MIME type as "application/force-download". The correct type to use in this situation is "application/octet-stream", and using anything else is merely relying on the fact that clients are supposed to ignore unrecognised MIME types and use "application/octet-stream" instead (reference: Sections 4.1.4 and 4.5.1 of RFC 2046).
Also according IANA there is no registered application/force-download type.
A clean example.
<?php
header('Content-Type: application/download');
header('Content-Disposition: attachment; filename="example.txt"');
header("Content-Length: " . filesize("example.txt"));
$fp = fopen("example.txt", "r");
fpassthru($fp);
fclose($fp);
?>
None of above worked for me!
Working on 2021 for WordPress and PHP:
<?php
$file = ABSPATH . 'pdf.pdf'; // Where ABSPATH is the absolute server path, not url
//echo $file; //Be sure you are echoing the absolute path and file name
$filename = 'Custom file name for the.pdf'; /* Note: Always use .pdf at the end. */
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file));
header('Accept-Ranges: bytes');
#readfile($file);
Thanks to: https://qastack.mx/programming/4679756/show-a-pdf-files-in-users-browser-via-php-perl
my code works for txt,doc,docx,pdf,ppt,pptx,jpg,png,zip extensions and I think its better to use the actual MIME types explicitly.
$file_name = "a.txt";
// extracting the extension:
$ext = substr($file_name, strpos($file_name,'.')+1);
header('Content-disposition: attachment; filename='.$file_name);
if(strtolower($ext) == "txt")
{
header('Content-type: text/plain'); // works for txt only
}
else
{
header('Content-type: application/'.$ext); // works for all extensions except txt
}
readfile($decrypted_file_path);

Categories