Create CSV and save on local/server automatically - php

I need to create a CSV file in PHP and that created file should be saved on local/server rather than asking to save.

For the PHP versions later than 5.1.0 you can use the built-in CSV functions.
<?php
$data_array = array (
array ('col1','col2'),
array ('2','2'),
array ('3','6'),
array ('4','2'),
array ('6','5')
);
$fp = fopen('csvfile.csv', 'w');
foreach ($list as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);
?>
This code will do the trick
$data_array = array (
array ('1','2'),
array ('2','2'),
array ('3','6'),
array ('4','2'),
array ('6','5')
);
$csv = "col1,col2 \n";//Column headers
foreach ($data_array as $record){
$csv.= $record[0].','.$record[1]."\n"; //Append data to csv
}
$csv_handler = fopen ('csvfile.csv','w');
fwrite ($csv_handler,$csv);
fclose ($csv_handler);
echo 'Data saved to csvfile.csv';
but be sure about permission of csvfile so your script can create or write on it

Related

Save generated csv file to sub folder with php

I have tried to store the created csv file to another subfolder instead of getting stored where the php-file is located but with no luck. Have anyone solved this by changing fopen? This is my code:
<?php
$data_array = array (
array ('test', $_POST['test'],)
);
$csv = "col1,col2 \n";//Column headers
foreach ($data_array as $record){
$csv.= $record[0].','.$record[1]."\n"; //Append data to csv
}
$csv_handler = fopen ('order_'.date('m-d-Y_hia').'.csv','w');
fwrite ($csv_handler,$csv);
fclose ($csv_handler);
echo 'Data saved to csvfile.csv';
?>

write CSV with fputcsv

When I use this code, the new CVS file get "" by text with blanks:
//array in csv schreiben
$daten = array_filter($daten);
$csv_array[] = $daten;
$fp = fopen("golfDBtest.csv", 'w');
foreach ($csv_array as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);
The original CSV:
Reit im Winkel,69.8,131,68.4,126,71.6,129,69.5,127,70,7,4,5,4,13,5,3,4,1,4,9,4,17,3,15,3,11,4,10,4,14,4,18,3,4,4,2,4,8,3,16,5,6,3,12,5,00438640798250
The new file:
"Reit im Winkl",69.8,131,68.4,126,71.6,129,69.5,127,70,7,4,5,4,13,5,3,4,1,4,9,4,17,3,15,3,11,4,10,4,14,4,18,3,4,4,2,4,8,3,16,5,6,3,12,5,00438640798250
How can I write a CVS without ""?
Thanks
According to the documentation of fputcsv function it has a default enclosure value: ". Setting this so blank should help your cause.
I'd say this should work:
//array in csv schreiben
$daten = array_filter($daten);
$csv_array[] = $daten;
$fp = fopen("golfDBtest.csv", 'w');
foreach ($csv_array as $fields) {
fputcsv($fp, $fields, ",", "");
}
fclose($fp);

convert a 2d array into a csv file in php

I'm try to convert a 2d array into a csv file . but i got errors
array to string conversion in line 7
here is my code
<?php
$export_arr =$_POST['dataString'];
$fp = fopen('file.xls', 'w');
foreach ( $export_arr as $line ) {
//$val = explode(",", $line);
fputcsv($fp, $export_arr);
}
fclose($fp);
?>
i want put the values in each cell in the xls file
any ideas
Change this line:
fputcsv($fp, $export_arr);
To:
fputcsv($fp, $line);
It should be:
fputcsv($fp, $line);

Saving more than one array in sequence in CSV file

I am using this function to generate CSV file
function array_to_csv($input, $delimeter=',', $linebreak="\n", $enclosure='"') {
if (!is_array($input))
die("Please provide an array of data.");
// $keys = array_keys($input);
$values = array_values($input);
foreach ($values as $k => $v) {
// Not refactored
$values[$k] = $enclosure.str_replace($enclosure, $enclosure.$enclosure, $v).$enclosure;
}
//print implode($delimeter, $keys);
//print $linebreak;
//print implode($delimeter, $values);
$write = implode($delimeter, $values);
$file='abc.csv';
$fp = fopen($file, "w");
fwrite($fp, $write);
fclose($fp);
}
In php I call this function as:
array_to_csv($title, $delimeter=',', $linebreak="\n", $enclosure='"');
$title is my array I am passing to the function. Till this point, everything is okay, file is successfully saved.
Now I want to writing CSV file using same function while calling more than 1 function such that:
1: first element of array1 is written in file
2: first element of array2 is written in file
3: first element of array3 is written in file
How can I accomplish this task? Have any one idea about it?

How to replace one line in php?

I have test.txt file, like this,
AA=1
BB=2
CC=3
Now I wanna find "BB=" and replace it as BB=5, like this,
AA=1
BB=5
CC=3
How do I do this?
Thanks.
<?php
$file = "data.txt";
$fp = fopen($file, "r");
while(!feof($fp)) {
$data = fgets($fp, 1024);
// You have the data in $data, you can write replace logic
Replace Logic function
$data will store the final value
// Write back the data to the same file
$Handle = fopen($File, 'w');
fwrite($Handle, $data);
echo "$data <br>";
}
fclose($fp);
?>
The above peace of code will give you data from the file and helps you to write the data back to the file.
Assuming that your file is structured like an INI file (i.e. key=value), you could use parse_ini_file and do something like this:
<?php
$filename = 'file.txt';
// Parse the file assuming it's structured as an INI file.
// http://php.net/manual/en/function.parse-ini-file.php
$data = parse_ini_file($filename);
// Array of values to replace.
$replace_with = array(
'BB' => 5
);
// Open the file for writing.
$fh = fopen($filename, 'w');
// Loop through the data.
foreach ( $data as $key => $value )
{
// If a value exists that should replace the current one, use it.
if ( ! empty($replace_with[$key]) )
$value = $replace_with[$key];
// Write to the file.
fwrite($fh, "{$key}={$value}" . PHP_EOL);
}
// Close the file handle.
fclose($fh);
The simplest way (if you are talking about a small file as above), would be something like:
// Read the file in as an array of lines
$fileData = file('test.txt');
$newArray = array();
foreach($fileData as $line) {
// find the line that starts with BB= and change it to BB=5
if (substr($line, 0, 3) == 'BB=')) {
$line = 'BB=5';
}
$newArray[] = $line;
}
// Overwrite test.txt
$fp = fopen('test.txt', 'w');
fwrite($fp, implode("\n",$newArray));
fclose($fp);
(something like that)
You can use Pear package for find & replace text in a file .
For more information read
http://www.codediesel.com/php/search-replace-in-files-using-php/

Categories