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
Related
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 ?
I want to download a file using PHP from an absolute path using the header() and readfile() methods. It used to work before, but doesn't anymore.
I've tried changing the Content-type, but nothing worked.
$p = $_GET['plugin'];
$v = $_GET['version'];
if (isset($p) && isset($v)) {
if (in_array($p, getPlugins($_SESSION['username']))) {
$url = '/home/uploads/' . $p . '/' . $v . '.jar';
header('Content-Description: File Transfer');
header("Content-type: application/java-archive");
header('Content-Disposition: attachment; filename="' . $p . ' ' . $v . '.jar"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-length: ' . filesize($url));
readfile($url);
//header('Location: plugins.php');
exit();
}
}
It should download the jar file that should be named something like "MyPlugin Snapshot-1.0.0.jar". Instead, it prints the whole jar file to the page (see image)
Try using the following content type:
header("Content-type: application/octet-stream");
Did that do the trick?
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.
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 . '"');
My goal is to insert a variable's value directly into a string without concatenation using the . operator.
I tried like this:
$filename = 'some_file_name';
header('Content-disposition: attachment; filename=$filename');
But this gives me filename=$filename, not filename=some_file_name.
How do I achieve this? What am I missing?
$filename = "path/to/file.txt";
header("Content-disposition: attachment; filename=$filename");
$filename = 'test.txt';
header('Content-disposition: attachment; filename=' . $filename);
Try this:
header('Content-Disposition: attachment; filename="' . $document . '"');
Where $document is your variable. Added double quotes are here so you can have whitespaces in
your filenames.