I'm trying to create a PHP page that opens the query and automatically updates the csv file with a new value.
I disc location I have file "graph.csv" with date, and COUNT from SQL Query.
I try open this file and add new row everytime when page is loaded. But, currently browser try save and overwrite file manual. php and csv file are in the same location.
<?php
$connect = oci_connect("login","pass","db");
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=graph.csv');
$output = fopen("php://output", "w");
fputcsv($output, array('TIME','Orders'));
$query = "SELECT column1, column2 FROM table1";
$result = oci_parse($connect, $query);
$r=oci_execute($result);
while ($row = oci_fetch_array($result, OCI_BOTH))
{
$handle = fopen("graph.csv", "a");
$handle = fputcsv($output, $row);
}
$handle = fclose($output);
?>
Related
I have this code
$dateFile = "data.txt";
$data = $this->Setting->Loop("data");
foreach($data->result() as $dat){
$dataString = "USERNAME| ".$dat->user." / DATA| ".$dat->values_text.".\n";
file_put_contents($dateFile,$dataString);
}
header('Content-Type: application/text');
header('Content-Disposition: attachment; filename="'.$dateFile);
echo file_get_contents($dateFile);
which get data from table data and insert it into file called data.txt with this format
USERNAME| qwq / DATA| www.
My problem is that the code take just one record of data because the data stored in a single string, how can I make it get all records?
Edit #1
I found a solution and this is the new working code
$dateFile = "data.txt";
$data = $this->Setting->Loop("data");
$dataContent = array();
$i = 0;
foreach($data->result() as $dat){
$i++;
$dataContent[$i] = "USERNAME| ".$dat->user." / DATA| ".$dat->values_text.".\n";
}
file_put_contents($dateFile,$dataContent);
header('Content-Type: application/text');
header('Content-Disposition: attachment; filename="'.$dateFile);
echo file_get_contents($dateFile);
Try this code:
$filename = __dir__.'test.php';
$data = 'This is sample text';
fopen($filename, 'w') or die('Cannot open file: '.$filename);
fwrite($handle, $data);
Hope this will help you
$filename = "PM.xls";
$exists = file_exists('PM.xls');
if($exists)
{
unlink($filename);
}
$filename = "PM.xls";
$fp = fopen($filename, "wb");
$sql = "SELECT DATE_FORMAT(jobcard.Open_date_time,' %d-%b-%y') AS datee,vehicles_data.Frame_no, jobcard.Jobc_id,jobcard.serv_nature,jobcard.Customer_name,jobc_invoice.Lnet, jobc_invoice.Pnet, jobc_invoice.Snet, jobcard.Mileage,customer_data.cust_type,vehicles_data.model_year,jobcard.Veh_reg_no,jobcard.comp_appointed, customer_data.mobile,IF(variant_codes.Make IS NULL,'Others',variant_codes.Make) as make FROM `jobcard` LEFT OUTER JOIN jobc_invoice ON jobcard.Jobc_id=jobc_invoice.Jobc_id LEFT OUTER JOIN vehicles_data ON jobcard.Vehicle_id=vehicles_data.Vehicle_id LEFT OUTER JOIN variant_codes ON vehicles_data.Model=variant_codes.Model LEFT OUTER JOIN customer_data ON jobcard.Customer_id=customer_data.Customer_id ORDER BY `make` ASC";
$result=mysqli_query($conn,$sql) or die(mysqli_error($sql));
$schema_insert = "";
$schema_insert_rows = "";
while($row = mysqli_fetch_row($result))
{
$insert = $row[0]. "\t" .$row[1]. "\t".$row[2]. "\t".$row[3]. "\t".$row[4]. "\t".$row[5]. "\t".$row[6]. "\t".$row[7]. "\t".$row[8]. "\t".$row[9]. "\t".$row[10]. "\t".$row[11]. "\t".$row[12]. "\t".$row[13]. "\t".$row[14]. "\t".$row[15];
$insert .= "\n"; // serialize($assoc)
fwrite($fp, $insert);
}
Above code successfully creates the file on server but can't replace it, Instead of creating the file on server i want it download the file on user end. Should I use any excel library as I have to name excel sheet as well.
First, you creating TSV file with XLS extension. If you need to create XLS document, you should use PHPExcel or so.
If CSV is enough for you, there are fputcsv available since 5.1 and you can stream file data directly to the user agent.
$filename = "PM.csv";
header('Content-Description: File Transfer');
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="'.addcslashes($filename, '\\"').'"');
$fp = fopen('php://output', 'wb');
// ...
while($row = mysqli_fetch_row($result))
{
fputcsv($fp, $row, ',', '"');
}
fclose($fp);
When I export data to csv file and open file with wordpad myfield Sr. No. looks like Sr. No. I don't want double space after Sr. and also don't wanr space after No. Code is as follow.
$filename = "file.csv";
$fp = fopen('php://output', 'w');
$array = array('Sr. No.','Name','DOB','Address');
$header = str_replace('',' ', $array);
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename='.$filename);
fputcsv($fp, $header,',',' ');
$query = "select * from registratin";
$result = mssql_query($query);
$i = 1;
while($row = mssql_fetch_row($result)) {
$row = array_merge(array($i), $row);
fputcsv($fp, $row);
$i++;
}
If yo don't want your headers line to conform to the rules for a CSV, then just write that one line using fwrite() rather than fputcsv()
$headerLine = 'Sr. No,Name,DOB,Address';
fwrite($fp, $headerLine);
I have the following script that will export data from my database using php to csv file. Everything works fine, except when I try to open the file in excel, I get "file is corrupt". When I run this code it shows the error - "The file is corrupted and can not be opened." Thanks in advance!
<?php
// Connection
include_once('conn.php');
$sql = "select * from info";
$qur = mysql_query($sql);
// Enable to download this file
$filename = "sampledata.csv";
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Type: text/csv");
$display = fopen("php://output", 'w');
$flag = false;
while($row = mysql_fetch_assoc($qur)) {
if(!$flag) {
// display field/column names as first row
fputcsv($display, array_keys($row), ",", '"');
$flag = true;
}
fputcsv($display, array_values($row), ",", '"');
}
fclose($display);
exit;
?>
I found the answer of my own question. Just need to add the ob_clean() line before we call the headers to download csv file to clear the output buffer. This will solve the error of not opening csv file in excel.
I've have been successful in exporting my database to csv as a downloadable file. However what I now need to do is instead of creating a straight .csv file that's downloaded I need it to just save to a folder called "csv" on the server. Here is my code for the current export. I need help in the saving it to server. I'm not saving the data correctly.
// output headers so that the file is downloaded rather than displayed
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=data.csv');
// create a file pointer connected to the output stream
$output = fopen('php://output', 'w');
// output the column headings
fputcsv($output, array('tax_class_id','_product_websites'));
// fetch the data
mysql_connect('localhost:3036', 'x', 'x');
mysql_select_db('lato');
$rows = mysql_query('SELECT taxclass,productwebsite FROM product');
// loop over the rows, outputting them
while ($row = mysql_fetch_assoc($rows))
fputcsv($output, $row);
$filename = "data.csv"; // Trying to save file in server
file_put_contents("download/" . $filename, "$header\n$rows");
Why do write in streams, read it and than try to save the content?
I would do it in a smaller way:
//Open a file in write-mode (he creates it, if it not exists)
$fp = fopen('./my/path/on/server/data.csv', 'w');
// output the column headings
fputcsv($fp, array('tax_class_id','_product_websites'));
// fetch the data
mysql_connect('localhost:3036', 'x', 'x');
mysql_select_db('lato');
$rows = mysql_query('SELECT taxclass,productwebsite FROM product');
// loop over the rows, outputting them
while ($row = mysql_fetch_assoc($rows))
fputcsv($fp, $row);
//close the handler
fclose($fp);