in my application i need to download a excel file created with some data in a db(postgresql), when i force the download sending headers
function createExcel($filename, $arrydata) {
$excelfile = "xlsfile://tmp/".$filename;
$fp = fopen($excelfile, "wb");
if (!is_resource($fp)) {
die("Error al crear $excelfile");
}
fwrite($fp, serialize($arrydata));
fclose($fp);
header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header ("Last-Modified: " . gmdate("D,d M YH:i:s") . " GMT");
header ("Cache-Control: no-cache, must-revalidate");
header ("Pragma: no-cache");
header ("Content-type: application/x-msexcel; charset=utf-8");
header ("Content-Disposition: attachment; filename=\"" . $filename . "\"" );
readfile($excelfile);
error_log("headers");
}
it downloads but it reads it as a cvs file and ask me for formating, but when i go to /tmp in my browser and click the file it downloads it as an excel file and shows the data just as i have put it in the code
Related
I have a file in excel and I am converting it to pdf using phpexcel. It is displaying russian letters as ????
Here is the code that is responsible for conversion:
public function exportToExcel($phpExcelObject)
{
header("Expires: Mon, 1 Apr 1974 05:00:00 GMT");
header("Last-Modified: " . gmdate("D,d M YH:i:s") . " GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
header("Content-type: application/vnd.ms-excel", 'charset=UTF-8');
header("Content-Disposition: attachment; filename=matrix.xls");
$objWriter = new PHPExcel_Writer_Excel5($phpExcelObject);
$objWriter->save('php://output');
}
Could you please help me?
First, check the folder which includes fonts supported by domPDF. Then, If there are any font which ends with .ttf, apply this font-family to the corresponding cells of PhpExcel Object. Then it works.
$sheet->getStyle('A1')->getFont()->setName('dejavu sans');
I am getting this error:
Warning: getimagesize(http://dev.clearcaresolutions.local/viewimage.php?file=screenshot_from_2016-11-29_12-12-06.png&type=form):
failed to open stream: HTTP request failed! HTTP/1.0
500 Internal
Server Error in /var/www/vhosts/dev.clearcaresolutions.local/current/modules/tasks/edit_log.php on line 845
This is the response code (please don't judge, I didn't write this, I have to live with it :/ ):
$fSize = filesize($dataPath.'/'.$file);
$fType = cFile::getMimeType($file);
// write the HTML headers
header ("X-Frame-Options: sameorigin");
#header("X-Frame-Options: deny"); # on
header ("X-XSS-Protection: 1; mode=block"); # off
header ("X-Content-Type-Options: nosniff"); # on
// BEGIN extra headers to resolve IE caching bug (JRP 9 Feb 2003)
header("Pragma: "); # on
header("Cache-Control: "); # on
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); # on
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); # on
header("Cache-Control: no-store, no-cache, must-revalidate"); //HTTP/1.1 # on
header("Cache-Control: post-check=0, pre-check=0", false); # on
// END extra headers to resolve IE caching bug
header("MIME-Version: 1.0");
header("Content-Description: File Transfer");
header("Content-Length: {$fSize}"); # ---
header("Content-Type: {$fType}"); # on
header("Content-Transfer-Encoding: binary"); # ---
header("Content-disposition: {$mode}; filename={$file}"); # on
echo readfile($dataPath.'/'.$file);
$fSize and $fType are correct, so this is not the issue.
This is how I call the response:
$protocol = stripos($_SERVER['SERVER_PROTOCOL'],'https') === true ? 'https://' : 'http://';
(line 845) list($width, $height) = getimagesize($protocol.$_SERVER['SERVER_NAME'].$field['fieldsrc']);
When I use $field['fieldsrc'] (which looks something like this:
/viewimage.php?file=screenshot_from_2016-11-29_12-12-06.png&type=form
as a data for src inside image tag, the image is shown properly, so I am guessing the response is correct. Any idea what is wrong here?
readfile already outputs the content of the file. Remove the echo in front of it. It should be only
readfile($dataPath.'/'.$file);
i want to create a link to download an excel file from the root in server computer, using php. so i wrote a simple code as below,
<?php
header("Content-disposition: attachment; filename=example.xls");
header("Content-type: application/vnd.ms-excel");
readfile("example.xls");
?>
it then can be downloaded however when i want to open it, i got the error saying the file i downloaded is in a different format than specified by the file extension. i also tried the same method with jpeg file and didnt get the same error but when i click it, it shows nothing. can someone help me? im not very good with programming. thank you in advance!
Try this
$file='example.xls'; $filesize=filesize($file);
header("Expires: Thu, 19 Nov 1981 08:52:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
header("Pragma: no-cache");
header('Content-type: application/vnd.ms-excel');
header('Content-Disposition: inline; filename="'.basename($file).'"');
header("Content-Length: " . $filesize);
$fh = fopen("$file, "rb");
// output file
while(!feof($fh))
{
# output file without bandwidth limiting
print(fread($fh, filesize($file)));
}
fclose($fh);
<?php
$docName = "testdoc";
header("Expires: Mon, 26 Jul 2020 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("Content-Type: application/msword; charset=ISO-8859-1");
header("Content-Disposition: attachment; filename=\"".$docName.".doc");
#/ Create and format your text using HTML, its simple stuff ... trial and error ;)
echo '<img src="logo.gif" />';
?>
Soumya Sarkar
Hi I got this following code from google to generate a Doc file by php. But I am unable to put any image there. Can anybody help me with it?
It requires the link, you will need to host the images online in order for the image to be shown on the doc.
Try this and it is working:-
echo "<img src='http://i.imgur.com/MJ5Sa.jpg' />";
I am trying to download a file using PHP. Now the file is downloaded , but it is not getting in the original format (extension missing). I can use the downloaded file after rename it using the original file extension. I am using the following code
header("Expires: 0");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("Content-type: application/".$result['ext']);
header('Content-length: '.filesize($file));
header('Content-disposition: attachment; filename='.$result['file_name']);
readfile($file);
exit;
where
$result['ext']="rar",
$file="file path to the uploaded folder".
I'm pretty sure you have to send the extension to the browser in the file name as part of your Content-disposition header.
For your code, you would have to change this:
header('Content-disposition: attachment; filename='.$result['file_name']);
to:
$filename = $result['file_name'] . '.' . $result['ext'];
header('Content-disposition: attachment; filename=' . $filename);