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);
Related
Im having a problem of exporting my csv. Yes it can export but when it exported the colmun name is included. How can i remove the first row (column name) after i exported?
Tried looking for other solution yet it doesnt fit on my program
<?php
//include database configuration file
include 'config2.php';
//get records from database
$query = $db->query("SELECT * FROM maternalproblem ");
if($query->num_rows > 0){
$delimiter = ",";
$filename = "maternalproblem" . date('Y-m-d') . ".csv";
//create a file pointer
$f = fopen('php://memory', 'w');
//set column headers
$fields = array('MPID', 'district_id', 'barangay_id', 'PID', 'tuberculosis', 'sakit','diyabetes','hika','bisyo');
fputcsv($f, $fields, $delimiter);
//output each row of the data, format =line as csv and write to file pointer
while($row = $query->fetch_assoc()){
$lineData = array($row['MPID'], $row['district_id'], $row['barangay_id'], $row['PID'], $row['tuberculosis'],$row['sakit'],$row['diyabetes'],$row['hika'],$row['bisyo']);
df.to_csv($filename , header=False);
fputcsv($f, $lineData, $delimiter);
}
//move back to beginning of file
fseek($f, 0);
//set headers to download file rather than displayed
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="' . $filename . '";');
//output all remaining data on a file pointer
fpassthru($f);
}
exit;
?>
I just need to export the data and not with the column name. Thank you
It would probably be simpler to just not put the column titles out into the file
So remove these lines
//set column headers
$fields = array('MPID', 'district_id', 'barangay_id', 'PID', 'tuberculosis', 'sakit','diyabetes','hika','bisyo');
fputcsv($f, $fields, $delimiter);
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);
?>
I just can't seem to make ÆØÅ give me right chars.
If I open Excel and then import from data and choice UTF-8 it will show correctly. but I can't ask my users to open excel and do this, so how do I make Excel open the file correctly the first time?
//Give our CSV file a name.
$csvFileName = 'example.csv';
//Open file pointer.
$fp = fopen('php://output', 'w');
//Loop through the associative array.
foreach($sortedData as $row){
//Write the row to the CSV file.
fputcsv($fp, $row,";");
}
//Finally, close the file pointer.
fclose($fp);
$response = $response->withHeader('Content-Type', 'text/csv; charset=utf-8');
$response = $response->withHeader('Content-Disposition', 'attachment; filename="file.csv"');
$stream = fopen('php://memory', 'r+');
return $response->withBody(new \Slim\Http\Stream($stream));
I'm writing to a CSV file using the following code:
header('Content-Type: application/excel');
header('Content-Disposition: attachment; filename="sample.csv"');
$data = array(
'aaa,bbb,ccc,dddd',
'123,456,789',
'"aaa","bbb"'
);
$fp = fopen('php://output', 'w');
foreach ( $data as $line ) {
$val = explode(",", $line);
fputcsv($fp, $val);
}
fclose($fp);
echo "Do something else";
exit();
I want to execute code after fclose($fp);. But in the above example echo "Do something else"; is included in the CSV file. How can I ensure that any code after the fclose is not included in the outputted csv file?
You could put the code that you want to run after the csv file is generated in a seperate file and then call that file to run using fopen() after the csv has been created.
You have included headers as content disposition. So you can NOT echo something. It will be always printed in the excel sheet.
However you can perform other tasks such as insert/update/select/delete in database and other things which does not print anything.
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.