How can I insert a value into a string without concatenation? - php

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.

Related

Using variable as file name doesn't work in php

So I have this code:
$pcbestand = date('Y-m-d h:i:s A') . ".xlsx";
$file = $pcbestand ;
header('Content-Disposition: attachment; filename=' . $file );
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Length: ' . filesize($file));
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate');
header('Pragma: public');
readfile($pcbestand);
$pcbestand make en .xlsx file with the current date and time, so I am trying to give this name to my save as dialog, but this is what I get:
as you can see the .xlsx part is missing. what am I doing wrong? please help.
if I download the file the extension is null.
The issue is probably that windows filenames cannot contain the character :. Try
$pcbestand = date('Y-m-d h_i_s A') . ".xlsx";
or even
$pcbestand = date('Y_m_d_h_i_s_A') . ".xlsx";
to stay consistent and remove spaces (spaces in filenames are usually not good too.)
Aside from using disallowed characters in your filename like : that must be removed, you will also need to quote the name or get rid of all the spaces:
header('Content-Disposition: attachment; filename="' . $file . '"' );
Try
$pcbestand = date('Y-m-d') .".xlsx";
insted of
$pcbestand = date('Y-m-d h:i:s A') . ".xlsx";

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.

Cant pass value as variable in this php script

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

PHP file Download without saving to my directory?

I have to create one text file and make it downloadable without saving to my directory on button click.
Below I have mentioned code of my file that generate file.
<?php
$data = "ffff dfjdhjf jhfjhf f f hlm hoejrherueirybgb,nvbd;ihrtmrn.";
$filename = "SU_Project_Startup.txt";
$fh = fopen($filename, "w+");
fwrite($fh, $data, strlen($data));
fclose($fh);
header('Content-type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $filename . '"');
ob_end_flush();
echo $data;
exit();
?>
Can you tell me which changes still I have to do in this code?
Thanks in advance
Try this headers:
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file");
header("Content-Type: application/json"); // here is your type
header("Content-Transfer-Encoding: binary");
This is an example of taking pictures of the url
you can try it, or put it into function
<?php
header('Content-type: application/octet-stream');
header('Content-Disposition: attachment; filename="File-name.jpg"');
$fileurl = 'http://upload.wikimedia.org/wikipedia/commons/thumb/c/cc/Ramses_II_at_Kadesh.jpg/120px-Ramses_II_at_Kadesh.jpg';
$data = file_get_contents($fileurl);
echo $data;
?>

How to download PDF file from blob oracle using PHP?

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.

Categories