Creating csv file with php - php

I want to create a csv file, but when I run the code, it returns a blank page and no csv file. I use PHP 5.
I use the following code:
<?php
$data = array ('aaa,bbb,ccc,dddd',
'123,456,789',
'"aaa","bbb"');
$fp = fopen('data.csv', 'w');
foreach($data as $line){
$val = explode(",",$line);
fputcsv($fp, $val);
}
fclose($fp);
?>
Thank you!

Its blank because you are writing to file. you should write to output using php://output instead and also send header information to indicate that it's csv.
Example
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="sample.csv"');
$data = array(
'aaa,bbb,ccc,dddd',
'123,456,789',
'"aaa","bbb"'
);
$fp = fopen('php://output', 'wb');
foreach ( $data as $line ) {
$val = explode(",", $line);
fputcsv($fp, $val);
}
fclose($fp);

#Baba's answer is great. But you don't need to use explode because fputcsv takes an array as a parameter
For instance, if you have a three columns, four lines document, here's a more straight version:
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="sample.csv"');
$user_CSV[0] = array('first_name', 'last_name', 'age');
// very simple to increment with i++ if looping through a database result
$user_CSV[1] = array('Quentin', 'Del Viento', 34);
$user_CSV[2] = array('Antoine', 'Del Torro', 55);
$user_CSV[3] = array('Arthur', 'Vincente', 15);
$fp = fopen('php://output', 'wb');
foreach ($user_CSV as $line) {
// though CSV stands for "comma separated value"
// in many countries (including France) separator is ";"
fputcsv($fp, $line, ',');
}
fclose($fp);

Just in case if someone is wondering to save the CSV file to a specific path for email attachments. Then it can be done as follows
I know I have added a lot of comments just for newbies :)
I have added an example so that you can summarize well.
$activeUsers = /** Query to get the active users */
/** Following is the Variable to store the Users data as
CSV string with newline character delimiter,
its good idea of check the delimiter based on operating system */
$userCSVData = "Name,Email,CreatedAt\n";
/** Looping the users and appending to my earlier csv data variable */
foreach ( $activeUsers as $user ) {
$userCSVData .= $user->name. "," . $user->email. "," . $user->created_at."\n";
}
/** Here you can use with H:i:s too. But I really dont care of my old file */
$todayDate = date('Y-m-d');
/** Create Filname and Path to Store */
$fileName = 'Active Users '.$todayDate.'.csv';
$filePath = public_path('uploads/'.$fileName); //I am using laravel helper, in case if your not using laravel then just add absolute or relative path as per your requirements and path to store the file
/** Just in case if I run the script multiple time
I want to remove the old file and add new file.
And before deleting the file from the location I am making sure it exists */
if(file_exists($filePath)){
unlink($filePath);
}
$fp = fopen($filePath, 'w+');
fwrite($fp, $userCSVData); /** Once the data is written it will be saved in the path given */
fclose($fp);
/** Now you can send email with attachments from the $filePath */
NOTE: The following is a very bad idea to increase the
memory_limit and time limit, but I have only added to make sure if anyone faces the problem of connection time out or any other.
Make sure to find out some alternative before sticking to it.
You have to add the following at the start of the above script.
ini_set("memory_limit", "10056M");
set_time_limit(0);
ini_set('mysql.connect_timeout', '0');
ini_set('max_execution_time', '0');

In case someone is still looking for an answer (a simple one), here is the simple way of creating a csv with PHP.
//Let's say you want your csv file to have something like this:
**Title 1** | **Title 2** | **Title 3**
Value 1 | Value 2 | Value 3
Value 4 | Value 5 | Value 6
//create a variable and add your data to it
$data = "TITLE 1, TITLE 2, TITLE 3 \n";
$data .= val1 .",". val2 .",". val3 \n ;
$data .= val3 .",". val4 .",". val5 \n ;
//you can use a loop to add dynamic data to this variable, if you want.
//give your file a name.
$fileName = 'myData.csv';
//add the file path where you want to store your csv file
//you can use **$_SERVER['DOCUMENT_ROOT']** in your file path. Avoid using absoultue paths like **../** this
$filePath = 'your_path_to' . $fileName;
$fp = fopen($filePath, 'w+');
fwrite($fp, print_r($data, true));
//Once the data is written, it will be saved in the path given.
fclose($fp);

I think this is the shortest way
$columns = [
'id',
'product_name',
'product_url',
'price',
'category'
];
$products = [
[1, 'product 1', 'https://example.com/product-1', '9.99', 'category 1'],
[2, 'product 2', 'https://example.com/product-2', '19.99', 'category 2'],
[3, 'product 3', 'https://example.com/product-3', '29.99', 'category 3'],
[4, 'product 4', 'https://example.com/product-4', '39.99', 'category 4'],
];
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="products.csv"');
echo implode(',', $columns) . PHP_EOL;
foreach ($products as $product){
echo implode(',', $product) . PHP_EOL;
}

Related

How do I seperate CSV headers and columns in PHP

I am trying to create a csv with PHP, that separate the the headers and columns.
Currently I am able to create the csv and dump the data, but each row in dumped into one cell.
The result is:
I expected this:
Here is my code
<?php
// Load the database configuration file
require_once('db/connect_db.php');
//$time = date('Y-m-d:').preg_replace("/^.*\./i","", microtime(true));
$time = date('Y-m-d');
$dteTimetamp = date('Y-m-d');
// Fetch records from database
$find = $conn->prepare("SELECT School, SchoolAddress, School_Email, Principle_Name, Reception_Number, QuantityFingerPrintScanner, InvoiceNumber from School");
$find->execute();
$udonr = "$dteTimetamp" . "Request";
//$filename = "$udonr.csv";
// Create a file pointer
$f = fopen(dirname(__FILE__).'/testfolder/'.$udonr.'.csv', 'w');
// Set column headers
$header = array('School Name', 'Contact Person', 'Contact Number', 'School Address', 'Number of scanners to deliver', 'Invoice Nuber');
fputcsv($f, $header);
// Output each row of the data, format line as csv and write to file pointer
while($row = $find->fetch(PDO::FETCH_ASSOC)){
$lineData = array($row['School'], $row['Principle_Name'], $row['Reception_Number'], $row['SchoolAddress'], $row['QuantityFingerPrintScanner'], $row['InvoiceNumber']);
fputcsv($f, $lineData);
// 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;
#FROSIT is right. Excel is kinda dumb about opening CSV files, especially ones with comma separator (ironically).
Your file looks good but if you need to have it automatically open in Excel (e.i. someone else will need to open it), you might want to find which one is the default separator for Excel and set that in your script.

Unable to insert data in CSV file PHP

I am in process of inserting data in the desired CSV file from another CSV file.
CSV file is creating fine with out any problem but its is not insert array data in file.
It only inserts header on the first row.
Below is code I am trying:
date_default_timezone_set('America/New_York');
set_time_limit(0);
ini_set("memory_limit", -1);
$realPath = realpath( dirname(__FILE__) );
$path = $realPath.'/3pltracking/';
$files = scandir($path);
$FilePath = $path.$files[2];
$result = array();
$date = date('m-d-Y_his');
if (file_exists($FilePath))
{
if (($handle = fopen($FilePath, "r")) !== FALSE)
{
$i=0;
while (($data = fgetcsv($handle, 10000, ",")) !== FALSE)
{
$i++;
if($i==1) continue;
//$list = array('$data[2],$data[25],$data[4],$data[30],$data[41],$data[27]');
echo $data[2].",".$data[25].",".$data[4].",".$data[30].",".$data[41].",".$data[27];
echo "<br>";
$list = array($data[2].",".$data[25].",".$data[4].",".$data[30].",".$data[41].",".$data[27]);
// the problem is here I believe as it is empty array if I check it outside while loop
}
fclose($handle);
$headers = array('ReferenceNumber', 'TotalCartons', 'ShipCarrier', 'TrackingNum', 'FreightPP', 'TotalWeight');
$fp = fopen($realPath.'\3pltracking\TrackingFiles\Tracking_File_'.$date.'.csv', 'w');
fputcsv($fp, $headers);
foreach ($list as $line) {
$val = explode(",", $line);
fputcsv($fp, $val);
}
fclose($fp);
} else {
$body = "File Not Found";
}
}
Here is my CSV file data:
TransactionNumber,CustomerName,ReferenceNumber,PurchaseOrderNumber,ShipCarrier,ShipService,ShipBilling,ShipAccount,EarliestShipDate,CancelDate,Notes,ShipToName,ShipToCompany,ShipToAddress1,ShipToAddress2,ShipToCity,ShipToState,ShipToZip,ShipToCountry,ShipToPhone,ShipToFax,ShipToEmail,ShipToCustomerName,ShipToDeptNumber,ShipToVendorID,TotalCartons,TotalPallets,TotalWeight,TotalVolume,BOLNum,TrackingNum,TrailerNum,SealNum,ShipDate,ItemNumber,ItemQuantityOrdered,ItemQuantityShipped,ItemLength,ItemWidth,ItemHeight,ItemWeight,FreightPP,WarehouseID,LotNumber,SerialNumber,ExpirationDate,Supplier,Cost,FulfillInvShippingAndHandling,FulfillInvTax,FulfillInvDiscountCode,FulfillInvDiscountAmount,FulfillInvGiftMessage,SoldToName,SoldToCompany,SoldToAddress1,SoldToAddress2,SoldToCity,SoldToState,SoldToZip,SoldToCountry,SoldToPhone,SoldToFax,SoldToEmail,SoldToCustomerID,SoldToDeptNumber,FulfillInvSalePrice,FulfillInvDiscountPct,FulfillInvDiscountAmt
242328,PARADIGM TRENDS,123810,40-402849,CUSTOMER PICK UP,LTL,FreightCollect,,,,,HG BUYING- JEFFERSON DC 884,HG BUYING- JEFFERSON DC 884,125 LOGISTICS CENTER PKWY,,JEFFERSON,AL,30549,US,,,,,,,30,0,30,0.0174,,,,,,DOV3S,64,64,4,1,1,4,0,1,,,,,,0,0,,0,,,,,,,,,,,,,,,0,0,0
33,d,123810,40-402849,CUSTOMER PICK UP,LTL,FreightCollect,,,,,HG BUYING- JEFFERSON DC 884,HG BUYING- JEFFERSON DC 884,125 LOGISTICS CENTER PKWY,,JEFFERSON,AL,30549,US,,,,,,,30,0,30,0.0174,,,,,,DOV3S,64,64,4,1,1,4,0,1,,,,,,0,0,,0,,,,,,,,,,,,,,,0,0,0
44,PARAdgdfDIGM TRENDS,123810,40-402849,CUSTOMER PICK UP,LTL,FreightCollect,,,,,HG BUYING- JEFFERSON DC 884,HG BUYING- JEFFERSON DC 884,125 LOGISTICS CENTER PKWY,,JEFFERSON,AL,30549,US,,,,,,,30,0,30,0.0174,,,,,,DOV3S,64,64,4,1,1,4,0,1,,,,,,0,0,,0,,,,,,,,,,,,,,,0,0,0
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,BY3M,176,176,11,1,1,11,,,,,,,,,,,,,,,,,,,,,,,,,,0,0,0
There are so many ways of going about this... including str_getcsv($csvData). However here we'd go for something old-school & a bit twisted;-). We would create a Function that uses Regex and a Looping Construct to build-up the relevant CSV Data Structure. The Function below illustrates how. Also note that although we mentioned that this is a somewhat twisted, old-school approach: don't be fooled... because it does its thing still ;-).
<?php
$csvSourceFile = __DIR__ . "/1.csv";
$csvPreferredColumns = array('ReferenceNumber', 'TotalCartons', 'ShipCarrier', 'TrackingNum', 'FreightPP', 'TotalWeight');
$newCsvStrut = processCSVData($csvSourceFile, $csvPreferredColumns, __DIR__ . "/test.csv");
/**
* #param $csvSource // PATH TO THE MAIN CSV FILE
* #param array $csvPreferredColumns // ARRAY OF HEADER COLUMN-NAMES TO BE EXTRACTED FROM MAIN CSV
* #param null $newCSVFileName // NAME OF THE NEW CSV FILE TO BE CREATED.
* #return string
*/
function processCSVData($csvSource, array $csvPreferredColumns, $newCSVFileName=null){
// GET THE CONTENTS OF THE CSV FILE & STORE IT IN A VARIABLE
$csvData = file_get_contents($csvSource);
// SPLIT THE CONTENTS OF THE CSV FILE LINE BY LINE: THAT IS; AT THE END OF EACH LINE
// THUS CONVERTING THE DATA TO AN ARRAY...
$arrCsvLines = preg_split("#\n#", $csvData);
//FILTER OUT UNWANTED EMPTY VALUES FROM THE ARRAY
$arrCsvLines = array_filter($arrCsvLines);
// CREATE SOME VARIABLES TO BE USED WITHIN THE LOOP...
$strDataFinal = "";
$arrDataMain = $arrDataFinal = array();
// IF THERE IS MORE THAN ONE LINE IN THE ARRAY WE CREATED ABOVE,
// THEN CONTINUE PROCESSING THE DATA...
if($arrCsvLines && count($arrCsvLines)>0){
// SINCE THE HEADER IS ALWAYS THE FIRST LINE IN THE CHAIN,
// WE EXPLICITLY EXTRACT IT AND STORE IT IN A VARIABLE FOR LATER USE
$arrCsvHeaders = preg_split("#\,([\s\t]+)?#", $arrCsvLines[0]);
// NOW WE LOOP THROUGH ALL THE LINES WE CREATED BY SPLITTING THE CONTENTS
// OF THE CSV FILE AT THE END-OF-LINE BOUNDARY
foreach($arrCsvLines as $key=>$arrCsvLine){
// WE DON'T WANT ANYTHING AT INDEX "0" SINCE IT IS THE HEADER
// AND WE ALREADY DEALT WITH IT ABOVE....
// SO IF THE INDEX $key IS NOT 0, WE CAN CONTINUE PROCESSING
if($key != 0){
$arrDataTemp = array();
$arrTempCsvData = preg_split("#\,([\s\t]+)?#", $arrCsvLine);
foreach($arrTempCsvData as $iKey=>$sData){
$arrDataTemp[$arrCsvHeaders[$iKey]] = $sData;
}
$arrDataMain[] = $arrDataTemp;
}
}
foreach($arrDataMain as $iKey=>$subData){
$arrTempFinal = array();
foreach($subData as $key=>$data){
if(in_array($key, $csvPreferredColumns)){
$arrTempFinal[$key] = $data;
}
}
$arrDataFinal[] = implode(",\t", $arrTempFinal);
}
$arrDataFinal = array_merge( array(implode(",\t", $csvPreferredColumns)), $arrDataFinal);
$strDataFinal = implode("\n", $arrDataFinal);
if($newCSVFileName){
file_put_contents($newCSVFileName, $strDataFinal);
}
}
return $strDataFinal;
}
var_dump($newCsvStrut);
// PRODUCES SOMETHING SIMILAR TO THE LINES BELOW:
string 'ReferenceNumber, TotalCartons, ShipCarrier, TrackingNum, FreightPP, TotalWeight
123810, CUSTOMER PICK UP, 30, 30, , 0
123810, CUSTOMER PICK UP, 30, 30, , 0
123810, CUSTOMER PICK UP, 30, 30, , 0
, , , , , ' (length=204)

Edit specific record from a line in text file and PHP

I'm trying to make a simple news hit counter with PHP and text file. i wrote a simple code to check and read the file:
Text File:
//Data in Source File
//Info: News-ID|Hits|Date
1|32|2013-9-25
2|241|2013-9-26
3|57|2013-9-27
PHP File:
//Get Source
$Source = ENGINE_DIR . '/data/top.txt';
$Read = file($Source);
//Add New Record
foreach($Read as $News){
//Match News ID
if($News[0] == "2"){
//Add New Record and Update the Text File
}
}
Problem is i can't change the news hits! For example, i need change hits from second line from 241 to 242 and write it again in to the txt file.
I searched in this site and Google and tried some ways but i couldn't fix that.
At the least, you're forgetting to write the increment back to the file. Also, you're going to want to parse each row into columns you can work with (delimited by a pipe |).
Untested code, but the idea is:
$Source = ENGINE_DIR . '/data/top.txt'; // you already have this line
$Read = file($Source); // and this one
foreach ( $Read as $LineNum => $News ) { // iterate through each line
$NewsParts = explode('|',$News); // expand the line into pieces to work with
if ( $NewsParts[0] == 2 ) { // if the first column is 2
$NewsParts[1]++; // increment the second column
$Read[$LineNum] = implode('|',$NewsParts); // glue the line back together, we're updating the Read array directly, rather than the copied variable $News
break; // we're done so exit the loop, saving cycles
}
}
$UpdatedContents = implode(PHP_EOL,$Read); // put the read lines back together (remember $Read as been updated) using "\n" or "\r\n" whichever is best for the OS you're running on
file_put_contents($Source,$UpdatedContents); // overwrite the file
You could read the file and do something like this:
//Get Source
$Source = ENGINE_DIR . '/data/top.txt';
$Read = file($Source);
$News = array();
foreach ($Read as $line) {
list($id, $views, $date) = explode('|', $line);
$News[$id] = array(
'id' => $id,
'views' => $views,
'date' => $date,
);
}
At this point you have the array $News which contains every news item and you can change them as you wish (example: $News[2]['views'] = 242;).
The only thing you're missing now is the writing back to the file part, which is also easy.
$fh = fopen(ENGINE_DIR . '/data/top.txt', 'w'); //'w' mode opens the file for write and truncates it
foreach ($News as $item) {
fwrite($fh, $item['id'] . '|' . $item['views'] . '|' . $item['date'] . "\n");
}
fclose($fh);
And that's it! :)

Set File Extension in CakeResponse

I'm trying to generate a CSV file on the fly, depending on what the user selects as report output. Retrieving the data and writing it to a file using CakeResponse is done, however I'm struggling to set the file extension to '.csv', the file get downloaded as a normal text file.
CakePHP documentation suggests I do this:
$this->response->type('csv');
..but even this is not working, I'm still getting a text file. Can anyone shed some light? Please note, I'm not looking for new methods to generate a CSV file, I just want to change the extension. Thank you.
This is how I download the file:
$this->response->body($this->constructFileBody($logs));
return $this->response;
This is the method 'constructFileBody', although I think its beyond the scope of this question:
public function constructFileBody($logs = array()){
$content = "";
for($i = 0; $i < count($logs); $i++){
$row = $logs[$i]['EventLog'];
$line = $row['description'] . "," . $row['user'] . "," . $row['affected_user'] . "," . $row['report_title'] . "," . $row['date_created'] . "\n";
$content = $content . $line;
}
return $content;
}
As i saw your code, I don't think you used the header anywhere, try this code:
//create a file
$filename = "export_".date("Y.m.d").".csv";
$csv_file = fopen('php://output', 'w');
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename="'.$filename.'"');
$results = $this->ModelName->query($sql); // This is your sql query to pull that data you need exported
//or
$results = $this->ModelName->find('all', array());
// The column headings of your .csv file
$header_row = array("ID", "Received", "Status", "Content", "Name", "Email", "Source", "Created");//columns you want in csv file
fputcsv($csv_file,$header_row,',','"');
// Each iteration of this while loop will be a row in your .csv file where each field corresponds to the heading of the column
foreach($results as $result)
{
// Array indexes correspond to the field names in your db table(s)
$row = array(
$result['ModelName']['id'],
$result['ModelName']['received'],
$result['ModelName']['status'],
$result['ModelName']['content'],
$result['ModelName']['name'],
$result['ModelName']['email'],
$result['ModelName']['source'],
$result['ModelName']['created']
);
fputcsv($csv_file,$row,',','"');
}
fclose($csv_file);
Now look at your code and get the line of code mine which needs to be replaced.

Issue creating CSV in PHP MySQL

I have to run a pairing algorithm for a game and when the pairing is done, I display the pairing on HTML and create a csv file as well. Right now, once I am done with pairing, I create a multidimensional array to store the specific value and then pass it to the function in same php file to generate the csv file. However, doing this outputs the entire page code i.e. html and php code to the .csv file. Here is the code:
function performPairing()
{
....
$count=0;
$resultArray[][] = array();
while ($currrow = #mysql_fetch_row($result))
{
$playerone = $currrow;
$playertwo = #mysql_fetch_row($result);
$resultArray[$count][] = $playerone[1];
$resultArray[$count][] = $playerone[0];
$resultArray[$count][] = $playertwo[1];
$resultArray[$count][] = $playertwo[0];
$count++;
updateforeachrow($playerone, $playertwo);
}
generateDocument($resultArray, $count);
}
function generateDocument($resultArray, $count)
{
$output = fopen('php://temp/maxmemory'.(5*1024*1024), 'r+');
$columns = array('Player One Col1', 'Player One Col2', 'Player Two Col1', 'Player Two Col2');
fputcsv($output, $columns);
for ($index=0 ; $index <=$count; $index++)
{
fputcsv($output, $resultArray[$index]);
}
rewind($output);
$export = stream_get_contents($output);
fclose($output);
header('Content-type: application/octet-stream');
header('Content-Disposition: attachment; filename = "export.csv"');
echo $export;
}
However doing this outputs the entire html code to csv rather than specific rows. Can anyone please help me on this?
. make string
. output string to file
. send header with (is this allowed this way?)
use file_put_contents ( filename, str ), and than send it with headers.
Make code simpler :)

Categories