Using variable as file name doesn't work in php - 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";

Related

How can i setting utf-8 to csv file in php [codeigniter]

public function ExportCSV(){
$this->load->dbutil();
$this->load->helper('file');
$this->load->helper('download');
$delimiter = ",";
$newline = "\r\n";
$slash = "∕";
$filename = "testnaja.csv";
$query = "select * from student where room_id = 011";
$result = $this->db->query($query);
$data = $this->dbutil->csv_from_result($result, $delimiter, $newline);
force_download($filename, $data);
}
I want to generate excel file exporting data from my table to it.
The output in .xls and .csv
In csv output is:
Please help. Thanks.
Change your force_download from
force_download($filename, $data);
To this:
force_download($filename, "\xEF\xBB\xBF" . $data);
which makes the output UTF-8 + BOM so it's recognized by MS Excel
I don't know if I understood you correctly, but if your problem is that you'd like Excel to recognize your CSV file as being UTF-8 encoded, you'll have to add a UTF-8 BOM to your file. Just prepend your CSV output with
chr(239) . chr(187) . chr(191) // xEF xBB xBF
For example:
$data = chr(239) . chr(187) . chr(191) . $this->dbutil->csv_from_result($result, $delimiter, $newline);
If your problem is about forcing a download, perhaps https://stackoverflow.com/a/33593363/11354 is going to help.
To setup codeigniter to download CSV format, use the following code.
function _push_file($path, $name)
{
// make sure it's a file before doing anything!
if(is_file($path))
{
// required for IE
if(ini_get('zlib.output_compression')) { ini_set('zlib.output_compression', 'Off'); }
// get the file mime type using the file extension
$this->load->helper('file');
$mime = get_mime_by_extension($path);
// Build the headers to push out the file properly.
header('Pragma: public'); // required
header('Expires: 0'); // no cache
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Last-Modified: '.gmdate ('D, d M Y H:i:s', filemtime ($path)).' GMT');
header('Cache-Control: private',false);
header('Content-Type: '.$mime); // Add the mime type from Code igniter.
header('Content-Disposition: attachment; filename="'.basename($name).'"'); // Add the file name
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.filesize($path)); // provide file size
header('Connection: close');
readfile($path); // push it out
exit();
}
Duplicate from : Codeigniter Force download files
and also from : Download csv from codeigniter mysql

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

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 . '"');

Force 'save as' svg files

I've got the following file:
download.php
<?php
$filename = $_GET["filename"];
if ($filename) {
$path = "/svg_files/".$filnavn."";
header('Content-Transfer-Encoding: binary'); // For Gecko browsers mainly
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime($path)) . ' GMT');
header('Accept-Ranges: bytes'); // For download resume
header('Content-Length: ' . filesize($path)); // File size
header('Content-Encoding: none');
header('Content-Type: application/svg'); // Change this mime type if the file is not PDF
header('Content-Disposition: attachment; filename=' . $filename); // Make the browser display the Save As dialog
}
?>
It's located in the svg_files folder. But when I run:
myweb.com/svg_files/download.php?filename=file.svg
I just get an empty file.
You'll have to output the content of the file to the browser..
Use readfile or file_get_contents and echo or something similar.
As a sidenote: Be aware of so called directory traversal attacks. You might wanna take some precautions.
http://en.wikipedia.org/wiki/Directory_traversal_attack

Force download working, but showing invalid when trying to open locally

I wrote this function and everything works well till i try to open the downloaded copy and it shows that the file is invalid. Here is my function
function download_file() {
//Check for download request:
if(isset($_GET['file'])) {
//Make sure there is a file before doing anything
if(is_file($this->path . basename($_GET['file']))) {
//Below required for IE:
if(ini_get('zlib.output_compression')) {
ini_set('zlib.output_compression', 'Off');
}
//Set Headers:
header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $this->path . basename($_GET['file'])) . ' GMT');
header('Content-Type: application/force-download');
header('Content-Disposition: inline; filename="' . basename($_GET['file']) . '"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($this->path . basename($_GET['file'])));
header('Connection: close');
readfile($this->path . basename($_GET['file']));
exit();
}
}
}
EDIT: By invalid for example I download a picture and try to view it in iPhotos or Windows Picture Viewer and it says, File Format Unsupported. When I view it on the server it looks fine but after download it is corrupt.
Thanks Gumbo, tried that and it outputted:
Warning: gmdate() expects
parameter 2 to be long, string given
in C:\Program Files\Wamp
Server\www\TutToasterUpload\PHPClass.php
on line 83 lets see what
happens
Fixed this line:
//Added filemtime();
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime($this->path . basename($_GET['file']))) . ' GMT');

Categories