I have tried to export the mysql data result to excel. After click the Export button, the form will send parameter's value into doexport.php.
The result was appeared in console.
<?php
session_start(); //Start the session
include('install_lang_japanese.php');
//connect to database
$dbc=mysqli_connect(_SRV,_ACCID,_PWD,"QPL");
if(!$dbc){
die('Connect Error: ' . mysqli_connect_error());
}
if(isset($_POST['action'])){
switch($_POST['action']){
case 'senddatacar':
$start = mysqli_real_escape_string($dbc,$_POST['startdate']);
$end = mysqli_real_escape_string($dbc,$_POST['enddate']);
$sqlex = "SELECT * FROM table";
$result =mysqli_query($dbc,$sqlex) or die(_ERROR30.":".mysqli_error($dbc));
$filename="cardata".date('ymd').".xls";
header("Content-type: application/vnd.ms-excel; name='excel'");
header(sprintf("Content-Disposition: attachment; filename=$filename"));
header("Pragma: no-cache");
header("Expires: 0");
//Then echo table
break;
}
mysqli_close($dbc);
}
My problem is : i don't see any file has downloaded as excel
Response Header at console :
Cache-Control:no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection:keep-alive
Content-Disposition:attachment; filename=cardata170929.xls
Content-Length:2988
Content-Type:application/vnd.ms-excel; name='excel'
Date:Fri, 29 Sep 2017 05:02:19 GMT
Expires:0
Pragma:no-cache
Server:Apache/2.2.22 (Ubuntu)
Related
I've this code:
<?php
include 'connectDb.php';
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Mon, 26 Jul 2997 05:00:00 GMT");
header("Content-Type: text/html; charset=utf-8");
echo file_get_contents('li.gif');
sleep(3);
$id = $_GET['id'];
$row = $conn->query('SELECT picture,img FROM contacts WHERE id=\''.$id.'\' LIMIT 1');
$result = $row->fetch_assoc();
if(!empty($result['picture'])){
header("Content-type: image/".$result['img']);
echo($result['picture']);
}
What I'm trying to do: show li.gif till mysql has loaded the echo($result['picture']).
Is there a way to do this without javascript?
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);
I'm trying to code a function in PHP to export some data to Excel file.
The problem is if I save it to the server it does work, but if I try to send to the browser using php://output it just doesn't work. It doesn't even show the download window. I' ve been getting these as response:
PK����a�B%���a��������[Content_Types].xml͔]K�0���%��f�
"�v��R���kX�����m��+����4�<�'��2�jgs6�,+��v����Sz���a�����tr5^�=Bb�9+c����,��9��.T"�kXr/�J,���[.��`ck6�?h�\��,���ܠ}3�c�C+��9�-E��|c�j�BKPN�+�d��u��O1�
o��Ba +���G�
The headers are:
Response Headers
Cache-Control no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection Keep-Alive
Content-Disposition attachment;filename="Report.xlsx"
Content-Type application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
Date Wed, 29 May 2013 10:08:10 GMT
Expires Thu, 19 Nov 1981 08:52:00 GMT
Keep-Alive timeout=15, max=78
Last-Modified Wed, 29 May 2013 10:08:11 GMT
Pragma no-cache
Server Apache/2.2.14 (Ubuntu)
Transfer-Encoding chunked
X-Powered-By PHP/5.3.2-1ubuntu4.19
Request Headers
Accept */*
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Cookie PHPSESSID=075r4aaqrvcnbca5sshjvm0jq7; 87293ba76812d31889be0901b086dd73=5h4jriq5c7r9vdt3m2u2u9up43; d82d00149fafbe651c9ba75a9804bbc9=en-GB
Host 150.145.139.3:8889
Referer
User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:20.0) Gecko/20100101 Firefox/20.0
X-Requested-With XMLHttpRequest
Here's my code:
<?php
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Europe/Rome');
require_once 'Classes/PHPExcel.php';
/** PHPExcel_IOFactory */
include 'Classes/PHPExcel/IOFactory.php';
$target ='templates/';
$fileType = 'Excel2007';
$InputFileName = $target.'richiesta.xlsx';
$OutputFileName = $target .'Richiesta_'.$_SESSION['User'].'_'.$_SESSION['Last'].'_'.$dat.'.xlsx';
//Read the file (including chart template)
$objReader = PHPExcel_IOFactory::createReader($fileType);
//$objReader->setIncludeCharts(TRUE);
$objPHPExcel = $objReader->load($InputFileName);
//Change the file
$objPHPExcel->setActiveSheetIndex(0)
// Add data
->setCellValue('C3','10' )
->setCellValue('C4','20' )
->setCellValue('C5','30')
->setCellValue('C5','40' );
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, $fileType);
//$objWriter->save($OutputFileName); //This one WORKS FINE!!!
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/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="Report.xlsx"');
$objWriter->save('php://output'); //NOT WORKING :-(
$objPHPExcel->disconnectWorksheets();
unset($objPHPExcel);
exit;
I'm getting depressed about this problem once I have to finish the project this week.
I really appreciate any kind of help!
That's just from my code, as a hint, the problem seems to be with the Content-Type HTTP header:
if (strtolower($type) == 'excel2003') {
$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="' . $outFileName . '"');
header('Cache-Control: max-age=0');
} else {
$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel2007');
header('Content-Type: application/xlsx');
header('Content-Disposition: attachment;filename="' . $outFileName . '"');
header('Cache-Control: max-age=0');
}
Try using these header() calls:
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");;
header("Content-Disposition: attachment;filename=Report.xlsx");
header("Content-Transfer-Encoding: binary ");
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$filename = '1111';
ob_end_clean();
header("Content-Type: application/vnd.openxmlformats-
officedocument.spreadsheetml.sheet");
header('Content-Disposition:attachment;filename="'.$filename.'.xlsx"');
header("Cache-Control: max-age=0");
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('php://output');
exit();
I had the same problem, and it seems that a download must be opened in a different tab instead of just downloading in the same tab. Meaning you need to redirect into a new tab before initializing the download.
<?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' />";
Here is my issue. I am trying to call a page: foo.php?docID=bar and return a PDF to the screen which is stored as a BLOB in the DB.
Here is the portion of my code which actually returns the PDF:
$docID = isset($_REQUEST['docID']) ? $_REQUEST['docID'] : null;
if ($docID == null){
die("Document ID was not given.");
}
$results = getDocumentResults($docID);
if (verifyUser($user, $results['ProductId'])){
header('Content-type: application/pdf');
// this is the BLOB data from the results.
print $results[1];
}
else{
die('You are not allowed to view this document.');
}
This works perfectly fine in Firefox.
However, in IE, it doesn't show anything at all. If i'm on another page (i.e. google.com), and I type in the URL to go to this page, it will say it's done, but I will still have google.com on my screen.
I checked the headers for the responses from both firefox and IE. They are identical.
Does anyone have any suggestions? Need more information?
EDIT: If it helps at all, here's the response header and the first line of the content:
HTTP/1.1 200 OK
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Content-Length: 349930
Content-Type: application/pdf
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: PHP/5.1.2
Set-Cookie: PHPSESSID=cql3n3oc13crv3r46h2q04dvq4; path=/; domain=.example.com
Content-Disposition: inline; filename='downloadedFile.pdf'
X-Powered-By: ASP.NET
Date: Tue, 21 Apr 2009 16:35:59 GMT
%PDF-1.4
EDIT: Also, the page which pulls out the pdf file actually uses HTTPS instead of HTTP.
Thanks in advance,
~Zack
I figured out what the issue was. It's an IE bug dealing with IE, HTTPS and addons. (See here)
It was a caching issue. When I set:
header("Cache-Control: max-age=1");
header("Pragma: public");
(see here), the PDF was in cache long enough for the adobe reader add-on to grab it.
I had this issue too, i used the following which seems to work fine
header("Content-type: application/pdf");
header("Content-Length: $length");
header("Content-Disposition: inline; filename='$filename'");
Try this:
header("Content-Type: application/pdf");
header("Content-Disposition: inline; filename=foo.pdf");
header("Accept-Ranges: bytes");
header("Content-Length: $len");
header("Expires: 0");
header("Cache-Control: private");
Also, if you are using sessions, you can try setting
session_cache_limiter("none");
or
session_cache_limiter("private");
if ( USR_BROWSER_AGENT == 'IE' ) {
header( 'Content-Disposition: inline; filename="' . $name . '"');
header( 'Expires: 0' );
header( 'Cache-Control: must-revalidate, post-check=0, pre-check=0' );
header( 'Pragma: public' );
} else {
header( 'Content-Disposition: attachment; filename="' . $name . '"' );
header( 'Expires: 0' );
header( 'Pragma: no-cache' );
}
This was the only header I needed to change:
header("Pragma: public");
I think you need to add more headers.
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment; filename=THEFILENAME.pdf;");
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . strlen($results[1]));