Save exported csv to a location on app storage - php

I am trying to save some failed records as csv to my laravel app storage. I have gone through this php documentation but how to store the file in a desired location is my challenge.
$failedRecordArrayFile = arrayToCsv($failedRecordArray, "file101.csv");
function arrayToCsv($data, $fileName)
{
$fp = fopen($fileName, 'w');
foreach ($data as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);
}
I ran the code block above and checked for the file name "file101.csv" in my entire application but I couldn't find any result.

this would work...
<?php
function arrayToCsv($data, $fileName)
{
$fp = fopen($fileName, 'w');
foreach ($data as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);
}
$failedRecordArray = array(array('col1','col2','col3','col4'), array('This','is','a','test'));
$failedRecordArrayFile = arrayToCsv($failedRecordArray, "file101.csv");
?>
file101.csv
col1,col2,col3,col4
This,is,a,test

Related

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);

PHP array into a CSV file

my problem is, that I want to save an array into a CSV file.
Here my code:
//Webseite auslesen
$string = file_get_contents("***Zensiert***");
$helper = preg_match_all('!<a.*?href=\"([^\"]*)\"[^>]*>(.*?)</a>!', $string, $matches);
$fl_array = preg_grep('/^http.*/', $matches[1]);
echo '<pre>';
print_r($fl_array);
echo '</pre>';
$fp = fopen('file.csv', 'a');
foreach ($fl_array as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);
The file is generated, but without content?!
I didn't see the mistake...
Hope you can help me.
I would change some lines to those
$fp = fopen('file.csv', 'w');
fputcsv($df, array_keys(reset($fl_array))); //Put column names as the 1st row (you may comment this line)
foreach ($fl_array as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);
This opens file to write.
And in your code you probably want to change that foreach into: fputcsv($fp, $fl_array );
fputcsv($fp, $fields);
$fields isn't an array.. so that was the problem.

Strategies to interupt an asychronous function call to dump a variable

I have a variable that I am trying to examine its content that is called asynchronously, so I cannot do a normal var_dump($variable) for example. I have been trying to write the variable content to a file, but not having success. This is the function including the file write code. I'm not sure of the data type of the $dataobject. How can I determine its type and dump its content into the data.txt file?
public function insert_record($table, $dataobject) {
$dataobject = (array)$dataobject;
$columns = $this->get_columns($table);
$cleaned = array();
$fp = fopen('../../data.txt', 'w');
foreach ($dataobject as $field => $value) {
//fwrite($fp, print_r($field), print_r($value) );
// fwrite($fp, print($field), print($value) );
// fwrite($fp, var_export($value) );
}
fclose($fp);
}
fwrite($fp, var_export($value, true));

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/

mysql foreach fwrite

I use some code here, transfer mysql query data into json data and write into a file. where is the problem? why the file is zero kb?
while($row = mysql_fetch_array($Query)){
$arr = array ('name'=>$row['name']);
$jsondata = json_encode($arr);
$countfile="data.txt";
if(!file_exists($countfile))
{
fopen($countfile,"w");
}
$fp = fopen($countfile, 'r');
fwrite($fp, $jsondata);
fclose($fp);
}
Several things.
You dont need (and should avoid) to open the file in every iteration
You open the file read-only (r)
At all something like this should do it
$countfile="data.txt";
$fp = fopen($countfile, 'w');
while($row = mysql_fetch_array($Query))
{
$arr = array ('name'=>$row['name']);
$jsondata = json_encode($arr);
fwrite($fp, $jsondata);
}
fclose($fp);
Additional you append separate json structures into the file, what is probably not, what you want. You should first collect all you want to store into one json structure and save it then
$data = array();
while($row = mysql_fetch_array($Query))
{
$data[] = array ('name'=>$row['name']);
}
file_put_contents('data.txt', json_encode($data));
This feels more like what you are probably looking for.
Because you're reopening the file as read only
$fp = fopen($countfile, 'r');
try
$fp = fopen($countfile, 'w'); // to write
or
$fp = fopen($countfile, 'a'); // to append
you could also open the file for writing at the start, append your rows in a variable and then write it all together to the file.
$countfile="data.txt";
$fp = fopen($countfile, 'w');
while($row = mysql_fetch_array($Query))
{
$arr = array ('name'=>$row['name']);
$jsondata .= json_encode($arr) . "\n";
}
fwrite($fp, $jsondata);
fclose($fp);
You are opening the file read-only. You probably want 'w' or 'r+'.
You are opening the file for reading only
$fp = fopen($countfile, 'r');
You also dont need
if(!file_exists($countfile))
{
fopen($countfile,"w");
}
just use:
$fp = fopen($countfile, 'w');

Categories