Zend Framework: How to download file from mySql Blob field - mysql

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;
?>

Related

Yii2 failed passing Image from PHP into Exported Excel, it passing the PHP Script into Excel

I'm trying to insert image from Yii2 into exported Excel;
This is the code :
<?php
$logo = Html::img("#web/img/Logo.jpg", ['width' => '60px', 'height' => '50px', 'margin-top' => '-10px']);
$downloadedTime = $downloadTZ->format('Y-m-d');
$bulan = \Yii::t('app', $month);
$filename = 'Report'.'xls';
header("Content-Disposition: attachment; filename=\"" . $filename . "\"");
header("Pragma: no-cache");
header("Expires: 0");
header("Content-Type: application/force-download");
header("refresh:0;url=../master/index");
echo $logo;
echo "Name\t";
echo "Class\t";
echo "Age\t";
echo "\r\n";
?>
if I echo $logo; the printed in browser, but in Excel it will print the code not the image, just like this
Why does this happen? and How do I can solve this?
Thanks
I do that with PHPExcel class and then calling PHPExcel_Worksheet_Drawing;
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setName('Logo');
$objDrawing->setDescription('Logo');
$objDrawing->setPath('img/logo.png');
$objDrawing->setCoordinates('A1');
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
like this.

How to export html table content into spreadsheet?

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.

Backbone with CakePHP JSON response

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';

Number of times a file (e.g., PDF) was accessed on a server

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.

Download file without right click

I have an <a href> link on a page that points to a video file (mp4). When the visitor clicks the link, the video file opens in the browser window.
If you want to download the file, you have to "right click", and then "save link as..."
I want that so that on a single click file will download (so that I don't have to right click).
edit: What's the problem here?
<?php
header('Content-Description: File Transfer');
header('Content-Type: video/mp4');
header('Content-Disposition: attachment; filename=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();
readfile($file);
exit;
?>
Set the Content-Disposition header to attachment. For example:
Content-Disposition: attachment; filename=lolcats42.mp4