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 . '"');
Related
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"
I'm trying to download pdf/jpg/jpeg/png using PHP. Here is my code
$result= mysqli_query("SELECT * FROM upload WHERE upload_id='$id'");
$row = mysqli_fetch_array($result);
//get file extension
$filename = $row['upload_name'];
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if($ext=='jpg'||$ext=='jpeg'||$ext=='png' ||$ext=='pdf'){
echo "Download";
}
download.php
$name= $_GET['id'];
header('Content-Description: File Transfer');
header('Content-Type: application/force-download');
header("Content-Disposition: attachment; filename=\"" . basename($name) . "\";");
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($name));
ob_clean();
flush();
readfile("uploads/".$name); //showing the path to the server where the file is to be download
exit;
When I tried the above code, it can be download however, when I tried opening the pdf/jpg/png it says invalid. Is there anything wrong with my code? This is my first time using the download code. So I really dont know what is the problem.
And for this path as stated in the code below,
readfile("uploads/".$name);
How do I write it if my pdf/jpg/png files are saved here in my computer C:\Apache2.2\htdocs\epals\uploads
Please help thanks
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.
Like the question says, I can't seem to pass a value in a variable in the following script. If I echo the variable, it exists as expected. If i copy and paste the echoed value into the code where $my_var is, it works. But it wont work with $my_var ?!?!?
Context- code requires another file to create a pdf, attaches it to an email and sends it, and then displays it in the browser. Have removed most of the code for brevity
$my_var = $_POST['quote_number'];
$filename = 'Quote_no' . $my_var . '.pdf';
$file = $_SERVER['DOCUMENT_ROOT'] . '/quotes/' . $filename ;
require('instant_quote.php');
function send_quote($my_var) {
//my_function code
};
send_quote($my_var);
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);
The syntax highlighting in your example was helpful, you have incorrect matching quotes:
$filename = "Quote_no' . $my_var . '.pdf";
... should be:
$filename = 'Quote_no' . $my_var . '.pdf';
Don't know why this worked, but it did...
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $file . '"');
header('Content-Length: ' . filesize($filename));
#readfile($filename);
Just removed the transfer encoding and accept-ranges from the headers, and it started accepting my variable as a value... go figure
I want to download blob file from oracle, that is PDF file, this is my code to get and download file:
<?php
$conn = ocilogon('user', 'pass', '//localhost/XE');
$sql = "SELECT PDFFILE FROM TFILE";
$stid = ociparse($conn,$sql);
ociexecute($stid);
$rowResult = ocifetch($stid);
settype($arrayResult,"array");
if($rowResult != null){
header("Content-Type: application/octet-stream");
header('Content-Disposition: attachment; filename="' . OCIResult($stid,'PDFFILE') . '"');
header("Content-Length: " . filesize(OCIResult($stid,'PDFFILE')->load()));
header('Content-Disposition: attachment; header("Content-Transfer-Encoding: binary\n");
}
?>
but when i run this code,i not get pdf file..
something wrong with my code??
header("Content-Type: application/octet-stream");
header('Content-Disposition: attachment; filename="' . OCIResult($stid,'PDFFILE') . '"');
header("Content-Length: " . filesize(OCIResult($stid,'PDFFILE')->load()));
header('Content-Disposition: attachment; header("Content-Transfer-Encoding: binary\n");
You're sending the Content-Disposition header twice. You probably don't even need the second one, the client should know all it needs to know about the stream from the Content-Type header. Omit the second Content-Disposition so you're not over-writing the header that has the filename.