How to convert a html table content into an excel spreadsheet?
I got lot of codes there which are good in Chrome but not in Mozilla?
I need a browser compatible code for exporting html table content into spreadsheet.
You can use CSV format to export data to Excel which support CSV format.
function array2csv(array &$array)
{
if (count($array) == 0) {
return null;
}
ob_start();
$df = fopen("php://output", 'w');
fputcsv($df, array_keys(reset($array)));
foreach ($array as $row) {
fputcsv($df, $row);
}
fclose($df);
return ob_get_clean();
}
Then use this export function
function download_send_headers($filename) {
// disable caching
$now = gmdate("D, d M Y H:i:s");
header("Expires: Tue, 03 Jul 2001 06:00:00 GMT");
header("Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate");
header("Last-Modified: {$now} GMT");
// force download
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
// disposition / encoding on response body
header("Content-Disposition: attachment;filename={$filename}");
header("Content-Transfer-Encoding: binary");
}
This is the way you can export information to CSV format. Which is easily to open in Excel.
Related
here's my problem, i hope someone can help me :(
Problem Description:
I make a PHPscript to backup every day Google/Gmail contacts from a account.
I get a atom file but i want a google_csv file.
Steps to Reproduce:
After authentication (OAuth v3) API i get contactList with POST https://www.google.com/m8/feeds/contacts/default/full?access_token=XXXXXXXXX.
So i get a ATOM file but I want extract a google csv (with group....)
I try with
- contacts/default/full?alt=csv&access_token=XXXXXXXXX (KO)
- contacts/default/full?alt=google_csv&access_token=XXXXXXXXX (KO)
- contacts/default/full?out=google_csv&access_token=XXXXXXXXX (KO)
- https://www.google.com/s2/u/0/data/exportquery?ac=false&cr=true&ct=true&ev=true&f=g2&gp=true&hl=fr&id=personal&max=-1&nge=true&out=google_csv&sf=display&sgids=6%2Cd%2Ce%2Cf%2C17&st=0&type=4&tok=XXXXXXXXX (KO)
...
Is there any suggestion to convert atom file to google_csv file ? OR to directly get a google_csv with a exportquery from google ?
Thanks
Based from this SO ticket, you need to have a csv file that is a table, with well defined columns and the same number of columns in each row.
The script below will show you how to get the google contacts in .csv file. You need to get the data in form of string and use the scripts for printing them for spread sheet format.
<?php
$data = $_POST['email'];
$arry[] = explode(',', $data);
foreach ($arry as $row)
{
$arlength = count($row);
for ($i = 0; $i < $arlength; $i++)
{
$farry[] = explode(',', $row[$i]);
}
}
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=file.csv");
header("Pragma: no-cache");
header("Expires: 0");
$file = fopen('php://output', 'w');
fputcsv($file, array('Description'));
foreach ($farry as $row)
{
fputcsv($file, $row);
}
exit();
Check this tutorial.
However if I do return $response; all the data is perfectly displayed.
It seems like json_decode only works when I call the API via curl but that was when the API was in a separate app on another vhost. I'm re-integrating it into our main app but I'm unable to extract the data.
echo $response['error']
echo $response->error
return $response->error
none of the above works.
performing a foreach on $response, all I get is header info.
foreach ($response as $a)
{
echo $a;
}
returns
Cache-Control: no-cache Content-Type: application/json Date: Fri, 14 Feb 2014 09:24:02 GMT
my controller
$response = ApiHelper::getCcm($pid);
//convert raw json into array
$ja = json_decode($response,true);
var_dump($ja);
the apihelper
return Response::json(array(
'error' => false,
'data' => $data->toArray()
),200);
I want JSON response from cakePHP which i will render using backbone.js. But instead of JSON response i am getting default.ctp content also along with JSON response i dont't know why. Is there something which i can do not to include default.ctp content in JSON response?
here is my code to fetch JSON
<?php
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Cache-Control: no-store, no-cache, max-age=0, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
//header('Content-type: text/json');
header('Content-type: application/json');
header('Pragma: no-cache');
//header("X-JSON: ".$content_for_layout);
$response['status'] = $status;
//$response['data']['titleForLayout'] = $title_for_layout;
$response['data']['validationErrors'] = $this->validationErrors;
$response['data']['sessionFlash'] = $this->Session->read('Message.flash.message');
//$response['data']['data'] = $this->data;
$response['data'][$this->request->params['controller']]['output'] = isset($output)?$output:null;
$output = json_encode($response);
if (isset($this->params['url']['callback'])) {
echo $this->params['url']['callback'] . '(' . $output . ');';
} else {
echo $output;
}
?>
where data->output contains the rows fetched.
Please help me out.
I get JSON response but the problem is get default.ctp content surrounding the response which i don't want. is there a way to do it?
Looks like you want Request Handling
http://book.cakephp.org/2.0/en/core-libraries/components/request-handling.html
You need to set the layout to be ajax or an empty layout.
In the controller set this:
$this->layout = 'ajax';
I have a small website with several PDFs free for download. I use StatCounter to observe the number of page loads. It also shows me the number of my PDF downloads, but it considers only those downloads where a user clicks on the link from my website. But what's with the "external" access to the PDFs (e.g., directly from Google search)? How I can count those? Is there any possibility to use a tool such as StatCounter?
Thanks.
.htaccess (redirect *.pdf requests to download.php):
RewriteEngine On
RewriteRule \.pdf$ /download.php
download.php:
<?php
$url = $_SERVER['REQUEST_URI'];
if (!preg_match('/([a-z0-9_-]+)\.pdf$/', $url, $r) || !file_exists($r[1] . '.pdf')) {
header('HTTP/1.0 404 Not Found');
echo "File not found.";
exit(0);
}
$filename = $r[1] . '.pdf';
// [do you statistics here]
header('Content-type: application/pdf');
header("Content-Disposition: attachment; filename=\"$filename\"");
readfile($filename);
?>
You can use a log analyzer to check how many times a file was accessed. Ask you hosting provider if they provide access to access logs and a log analyzer software.
in php, it would be something like (untested):
$db = mysql_connect(...);
$file = $_GET['file'];
$allowed_files = {...}; // or check in database
if (in_array($file, $allowed_files) && file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
mysql_query('UPDATE files SET count = count + 1 WHERE file="' . $file . '"')
readfile($file);
exit;
} else {
/* issue a 404, or redirect to a not-found page */
}
You would have to create a way to capture the request from the server.
If you are using php, probably the best would be to use mod_rewrite.
If you are using .net, an HttpHandler.
You must handle the request, call statcounter and then send the pdf content to the user.
I am uploading files(any type) in MySql tables's blob field. Now I am able to get binary data from that field and when I print it, it shows binary data in firbug console. But I want to download that file as it was uploaded.
How can I convert this binary data into orignal file? How to do it in zend?
Thanks
You need to set the headers at minimum you need
<?php
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".$filename."\";");
echo $data;
exit;
?>
Or preferablly
<?php
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header ( "Content-Type: $filedatatype" );
header("Content-Disposition: attachment; filename=\"".$FileObj->name."\";");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".$filesize);
echo $data;
exit;
?>