File download does not start with JQuery - php

I'm trying to start the download of a file using JQuery. The file is created by a .php function.
The download DOES NOT start. It starts only if i open the .php file MANUALLY (commenting the isset and writing the data manually into the file).
This is the JQuery code:
$("#experimenter_dataset").click(function() {
var imei = $("#select_download_experimenters option:selected").text();
$.post("download_imei_activities.php", { imei:imei }).done(function( data ) {
});
});
This is the php code
if(isset($_POST["imei"])) {
$imei = $_POST["imei"];
$stmt = $pdo->prepare("SELECT * FROM performedactivity WHERE experimenter=:imei");
$stmt->bindParam(":imei", $imei, PDO::PARAM_STR);
//$output = $imei . ".txt";
$output = "";
if ($stmt->execute()) {
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$activity = $row["activity"];
$timestamp_start = $row["timestampstart"];
$timestamp_stop = $row["timestampstop"];
$date_hour_start = explode(" ", $timestamp_start);
$date_hour_stop = explode(" ", $timestamp_stop);
//$current = "";
$output .= $date_hour_start[0] . "\t" . $date_hour_start[1] . "\t" . $activity . "\t" . "START" . "\r\n";
$output .= $date_hour_stop[0] . "\t" . $date_hour_stop[1] . "\t" . $activity . "\t" . "END" . "\r\n";
//file_put_contents($output, $current, FILE_APPEND | LOCK_EX);
}
} else {
//echo json_encode($pdo->errorInfo());
}
// We'll be outputting a text file
header("Cache-Control: public");
header("Content-Description: File Transfer");
header('Content-type: text/plain');
header('Content-Disposition: attachment; filename='.$imei . ".txt");
header("Content-Transfer-Encoding: binary");
print($output);
//echo $output;
/*header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename= " . $output);
header("Content-Transfer-Encoding: binary");
readfile($output);*/
} else {
//echo "imei parameter is missing";
}

Related

PHP Fputcsv header not showing

I am trying to create a csv in php with products from magento but my header doesn't show up in the csv file, just the products.
If i put the fputcsv for the headings inside the foreach it display the header then one product then the header again and another product and so on...
##### Print product data ####################################################
$headings = ['category', 'manufacturer', 'productid', 'identifier', 'name', 'description', 'product_url', 'image_url', 'price','show_product', 'availability', 'delivery_cost'];
$fh = fopen('php://output', 'w');
ob_start();
fputcsv($fh, $headings);
foreach($ALL_PRODS as $productId) {
// If we've sent this one, skip the rest - this is to ensure that we do not get duplicate products
if (#$already_sent[$productId] == 1) continue;
$PRODUCT = array();
$PRODUCT = smfeed_get_product_details($productId);
if ($PRODUCT['show_product'] == 1 ) {
fputcsv($fh, $PRODUCT);
$string = ob_get_clean();
$filename = 'csv_' . date('Ymd') . '_' . date('His');
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: application/octet-stream");
header("Content-Disposition: attachment; filename=\"$filename.csv\";");
header("Content-Transfer-Encoding: binary");
$prod_count ++;
// Debuging
if (isset($_GET['debug'])) {
$cnt_prod ++;
if (#$_GET['stats'] == "off") {
}
else {
echo $cnt_prod . ".";
echo "\t" . number_format(microtime(true) - $time, 3) . "s \n";
$time = microtime(true);
echo "\t" . number_format(memory_get_usage()/1048576, 3) . "Mb\n";
}
}
// Limit displayed products
if ($limit > 0 && $prod_count >= $limit && !isset($_GET['pg'])) {
// Debuging
if (isset($_GET['debug'])) {
echo "\n" . $cnt_prod . "products displayed \n";
echo "\npage loaded in " . number_format(microtime(true) - $time_start, 3) . "s \n";
echo number_format(memory_get_usage()/1048576, 3) . "Mb\n";
}
exit;
}
}
$already_sent[$productId] = 1;
}
exit($string);
ob_start(); starts output buffering.
Buffer contains the header line.
After the first product output is done, output buffering is ended by ob_get_clean();
Since $string = ob_get_clean(); assignment is called in the loop, its content being rewritten by false value (since the buffering was ended before) therefore final exit($string); does not output anything.
There are many other issues in your code. For example, HTTP headers will be set over again and again for each item in the loop.

Wordpress - export a mysql table to csv with column headers

I am able to download a file in csv format for my table , but how to add column headers to the same file .
The current code is following -
// load wpdb
$path = $_SERVER['DOCUMENT_ROOT'];
include_once $path . '/wp-load.php';
global $wpdb;
$table = $_POST["table_name"];// table name
$file = 'database_csv'; // csv file name
$results = $wpdb->get_results("SELECT * FROM $wpdb->prefix$table",ARRAY_A );
if(count($results) > 0){
foreach($results as $result){
$result = array_values($result);
$result = implode(", ", $result);
$csv_output .= $result."\n";
}
}
$filename = $file."_".date("Y-m-d_H-i",time());
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header( "Content-disposition: filename=".$filename.".csv");
header("Pragma: no-cache");
header("Expires: 0");
print $csv_output;
exit;
I was able to do it by first getting column names and then assigning it to final output :
$table_name = $wpdb->prefix.$_POST["table_name"];// table name
$file = 'database_csv'; // csv file name
$results = $wpdb->get_results("SELECT * FROM $table_name",ARRAY_A );
// get column names
$query = "SELECT `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA`='".$wpdb->dbname."' AND `TABLE_NAME`='".$table_name."'";
$columnNamesList = $wpdb->get_results($query);
foreach ( $columnNamesList as $column_name ) {
$csv_output.=$column_name->COLUMN_NAME.",";
}
// remove last additional comma
$csv_output = substr($csv_output,0,strlen($csv_output)-1);
// start dumping csv rows in new line
$csv_output.="\n";
if(count($results) > 0){
foreach($results as $result){
$result = array_values($result);
$result = implode(", ", $result);
$csv_output .= $result."\n";
}
}
$filename = $file."_".date("Y-m-d_H-i",time());
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header( "Content-disposition: filename=".$filename.".csv");
header("Pragma: no-cache");
header("Expires: 0");
print $csv_output;
exit;

create a bibtex archive with PHP

I'm trying to create a code that, based on informations from BD, creates a bibtex archive. That's what I got:
<?php
include("classe/conexao.php");
session_start();
$_SESSION[id_tese_especifica] = $_GET['id'];
$result = pg_query("SELECT titulo, id, data, autor_nome FROM teses ORDER BY data DESC");
$arr = pg_fetch_array($result);
echo "#phdthesis{phpthesis,
author={" . $arr[0] . "},
title={" . $arr[6] . " " . $arr[3] . "},
month={" . $arr[2] . "}";
$name = $_GET['id'] . ".bib";
$file = fopen($name, 'a');
$text = "test (it doesn't appears on archive and I don't know why, so I used the echo above and worked, but this is what should be on archive, or isn't?)";
fwrite($file, $text);
readfile($file);
fclose($fp);
header('Content-Disposition: attachment; filename="' . $file . '"');
header('Expires: 0');
?>
After that, it downloads an archive named 'Resource id #6', why? The name should be based on this: $name = $_GET['id'] . ".bib".
Thanks!
Because filename is stored in a $name variable in your code:
header('Content-Disposition: attachment; filename="' . $name . '"');
And $file variable is a resource, connected with open file.
And by the way - you don't close the file properly.
fclose($fp); // $fp is NOT defined, your pointer is in $file variable
Proper code for closing is:
fclose($file);
Next, rearrange your code.
First of all - headers should be sent BEFORE any output.
What you currently have is some mix of errors, which accidentally show you something that you want.
Proper code should be:
$name = $_GET['id'] . ".bib";
// first of all - set proper headers:
header('Content-Disposition: attachment; filename="' . $name . '"');
header('Expires: 0');
// next - do a query
$result = pg_query("SELECT titulo, id, data, autor_nome FROM teses ORDER BY data DESC");
$arr = pg_fetch_array($result);
// use echo for testing purposes only
// cause echo considered as a content of your file
echo "#phdthesis{phpthesis,
author={" . $arr[0] . "},
title={" . $arr[6] . " " . $arr[3] . "},
month={" . $arr[2] . "}";
$fp = fopen($name, 'a');
$text = "test (it doesn't appears on archive and I don't know why, so I used the echo above and worked, but this is what should be on archive, or isn't?)";
fwrite($fp, $text);
fclose($fp); // don't forget to close file for saving newly added data
readfile($name); // readfile takes a filename, not a handler.
die(); // end your script cause in other case all other data will be outputted too

Form downloads same ZIP over and over

Thanks to the users community on this forum, I wrote a very simple web form that allows my user to view text files from within their Internet browser.
I have now two functions whereby the text files returned by the search are compressed into a ZIP. Here's my code
function getFilesFromSite() {
$result = null;
$ZIPresult = null;
if (empty($_POST['DBSite'])) { return null; }
$mydir = MYDIR;
$dir = opendir($mydir);
$DBSite = $_POST['DBSite'];
$getfilename = mysql_query("select filename from search_table where site='" . $DBSite . "'") or die(mysql_error());
while ($row = mysql_fetch_array($getfilename)) {
$filename = $row['filename'];
$result .= '<tr><td>' . $filename . '</td></tr>';
$ZIPresult .= basename($mydir) . '/' . $filename.' ';
}
if ($result) {
$result = "<table><tbody><tr><td>Search Results.</td></tr> $result</table>";
shell_exec("/bin/rm -f SearchResult.zip;/usr/bin/zip -9 SearchResult.zip ". $ZIPresult ." > /dev/null ");
//header for forced download
header("Pragma: public");
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
$fileName = 'SearchResult.zip';
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Transfer-Encoding: binary");
header('Content-type: application/zip');
header("Content-length: " . filesize($fileName));
header('Content-Disposition: attachment; filename="' . $fileName . '"');
ob_start(); // Starts output buffering.
readfile($fileName); // "Outputs" the file.
$content = ob_get_flush(); // Grabs the output and assigns it to a variable.
print base64_encode($content);
}
function getFilesFromError() {
//Just a copy paste from above with different input parameter...
}
The problem is that the ZIP file with the contents from whatever search was done first gets downloaded over and over again. For instance, the results from getFilesFromSite() will always get downloaded even though I did a search with getFilesFromError() afterwards.
I suspect my headers are incorrectly set but I am not sure where.
PS: The new ZipArchive() library/class is not available on our production environment so I chose to use the Unix utility ZIP instead.
Using Base64 was actually not working for reasons well stated here. Instead, I turned zlib compression off and reverted back to using binary as output format. Finally, I set Content-Type to application/octet-stream in my header. Everything is working fine now ; here's my code:
function getFiles() {
ini_set('zlib.output_compression', 'Off');
$result = null;
$ZIPresult = null;
$cleanup = null;
$output = null;
$fileName = null;
//remove old zip if any
$cleanup = shell_exec("/bin/rm -f SearchResult.zip");
error_log("SHELL OUTPUT=>" . $cleanup, 0);
//test
if (empty($_POST['DBRIDs'])) { return null; }
$mydir = MYDIR; // set from the CONSTANT
$dir = opendir($mydir);
$DBRIDs = $_POST['DBRIDs'];
$getfilename = mysql_query("select /*! SQL_CACHE */ filename from automation where rid in (" . $DBRIDs . ")") or die(mysql_error());
while ($row = mysql_fetch_array($getfilename)) {
$filename = $row['filename'];
$result .= '<tr><td>' . $filename . '</td></tr>';
$ZIPresult .= basename($mydir) . '/' . $filename.' ';
}
if ($result) {
$result = "<table><tbody><tr><td>Search Results.</td></tr> $result</table>";
$output = shell_exec("/usr/bin/zip SearchResult.zip ". $ZIPresult ." ");
error_log("SHELL OUTPUT=>" . $output, 0);
$fileName = 'SearchResult.zip';
error_log("ZIP FILENAME=>" . $fileName, 0);
if (file_exists($fileName)) {
//header for forced download
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($fileName));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($fileName));
ob_clean();
flush();
readfile($fileName);
exit;
}
}
return $result;
}
Thanks to all for taking the time!!

Is the File Transfer code correct in my PHP?

I have this page that is supposed to be a download for a song. The download works in firefox for me but in chrome and safari nothing happens..here is my code
public function download() {
if (isset($this->request->get['order_download_id'])) {
$order_download_id = $this->request->get['order_download_id'];
} else {
$order_download_id = 0;
}
$download_info = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_download od LEFT JOIN `" . DB_PREFIX . "order` o ON (od.order_id = o.order_id) WHERE o.customer_id = '" . (int)$this->customer->getId(). "' AND o.order_status_id > '0' AND o.order_status_id = '" . (int)$this->config->get('config_download_status') . "' AND od.order_download_id = '" . (int)$order_download_id . "'");
if ($download_info->row) {
$file = DIR_DOWNLOAD . $download_info->row['filename'];
$mask = basename($download_info->row['mask']);
$mime = 'application/octet-stream';
$encoding = 'binary';
if (!headers_sent()) {
if (file_exists($file)) {
header('Pragma: public');
header('Expires: 0');
header('Content-Description: File Transfer');
header('Content-Type: ' . $mime);
header('Content-Transfer-Encoding: ' . $encoding);
header('Content-Disposition: attachment; filename="' . ($mask ? $mask : basename($file)) . '"');
header('Content-Length: ' . filesize($file));
$file = readfile($file, 'rb');
print($file);
} else {
exit('Error: Could not find file ' . $file . '!');
}
} else {
exit('Error: Headers already sent out!');
}
}
}
I have tried all kinds of different things to get this to work but nothing is happening in the two browsers...any ideas or help will be appreciated...
readfile returns the number of bytes sent, and needs not to be printed out. You should remove the line print($file);. Otherwise, you'll send more bytes than the Content-Length header specifies, and that will lead some HTTP clients to discard your answer.
Also, consider strange file names such as
"\r\nLocation: http://evil.com\r\n\r\n<script>alert('XSS');</script>
Are you handling that correctly?
See your syntax near
header('Content-Disposition: attachment; filename="'.$file_name_with_space. '"');
OR it can be
header("Content-Disposition: attachment; filename='".$file_name_with_space."'" );
Here the game is in Quotes only it will be treated as part of the string if it is written properly else will crash.
It works in all browser. IE, FF, Chrome, SAFARI I checked it personally so goahead.

Categories