Create a CSV file with PHP - php

I need to create a CSV file from a PHP array, I'm aware of the fputcsv() function. I've a recollection that a better function exists for this kind of stuff but I can't find it nor remember it's name.
Does anyone know of such function or am I imagining?
I might be making some confusion with the fgetcsv() function and the str_getcsv() equivalent.

fputcsv is to write an array into a file with CSV format. fgetcsv is to read data from a file with CSV format and convert it into an array. And str_getcsv is to convert a string in CSV format into an array.
So fputcsv and fgetcsv are their respective inverses. And fgetcsv could be a function that uses str_getcsv.

Related

php read csv data as a string

Is there a function in php that lets you read a csv and return a string as it is.
I am aware of functions like fgetcsv etc that reads the csv and returns an array. But I was wondering if there is a way to get the full string as it is.
You must use the file_get_contents function
$data = file_get_contents('http://absoluteUrl');
or
$data = file_get_contents('local url');
Instead, to obtain an array of csv's lines use the file function.
You could use the file function:
$file = file("somefile.csv")
The you have every line in the variable file:
$file[0] = Firstline, aso.

php - converting csv into array using str_get_csv without physically uploading

I'm just trying to upload a csv file and convert it to an array.
I've read on a previous post here that you can use str_getcsv() without having to physically upload the csv file to the server.
Is this correct or do I have to store the file somewhere first before I can run str_getcsv() and then delete it afterwards?
Here's my simple code at the moment (I'm using php 5.3):
if (isset($_POST['file_upload'])){
$csv = str_getcsv($_POST['csvfile']);
echo '<pre>';
print_r($csv);
echo '</pre>';
}
You should use $_FILES array to check file upload.
Use this code:
if(isset($_FILES["csvfile"])) {
$csv=str_getcsv(file_get_contents($_FILES["csvfile"]["tmp_name"]));
echo '<pre>';
print_r($csv);
echo '</pre>';
}
By default the file is stored in a temp directory which can be accessed by $_FILES['file_name']['tmp_name']:
As you can see, the first parameter of str_getcsv method must be a string and for this you can use file_get_contents
$csv = str_getcsv(file_get_contents($_FILES['csvfile']['tmp_name']));
If you are submitting a file (using <input type="file">), you will need to use move_uploaded_file(). The file is already on disk (if the form submit was succesful), you just need to move it. In this case, it is most convenient to use fgetcsv() which opens the file and parses it into arrays as you go (see the example in the documentation for that function).
If you are not submitting a file but a text using, for example, <textarea></textarea>, then you do not have to write the string in the $_POST variable to disk but can use str_getcsv to parse the string into an array.

How to read a PHP file as text file to an array?

Here is my situation.Supposing I have a PHP file named as:
myfunctions.php
Is it possible to read the contents of that PHP file (myfunctions.php) in another script using available PHP functions?
I'm using the following functions without success in reading this PHP file:
file_get_contents()
file()
It simply returned blank. One of the possible successful method was to read this PHP file as text file but I do not know how to do this..If someone has some other methods, please share. Ideally I want the output to be an array so the data I would like to obtain will be easily manipulated for use.
Thanks.
If I were you, I will change the extension on the "myfunctions.php" to "myfunctions.txt" and use the theses functions
file_get_contents()
file()
But I don't know if you are allow to change the name of the document.
file() - Reads entire file into an array
fgets() - Gets line from file pointer
fread() - Binary-safe file read
readfile() - Outputs a file
file_put_contents() - Write a string to a file
stream_get_contents() - Reads remainder of a stream into a string
stream_context_create() - Creates a stream context
You need fread. To put it into an array, you might look at explode

How do you EXPLODE CSV line with a comma in value?

"AMZN","Amazon.com, Inc.",211.22,"11/9/2011","4:00pm","-6.77 - -3.11%",4673052
Amazon.com, Inc. is being treated as 2 values instead of one.
I tried this $data = explode( ',', $s);
How do I modify this to avoid the comma in the value issue?
You should probably look into str_getcsv() (or fgetcsv() if you're loading the CSV from a file)
This will read the CSV contents into an array without the need for exploding etc.
Edit- to expand upon the point made by Pekka, if you're using PHP < 5.3 str_getcsv() won't work but there's an interesting approach here which reproduces the functionality for lesser versions. And another approach here which uses fgetcsv() after creating a temporary file.
Use a dedicated CSV library. It's been explained over and over that parsing file formats like CSV manually is asking for trouble, because you don't know all the variations of CSV and all the rules to do it right.

How to parse file in php and generate insert statements for mysql?

In case of csv file we have fgetcsv in php to parse and get the output but in my case file is .dat and I need to parse it and store it into MySQL Database and so do we have any built in function in php like fgetcsv that can work in similar fashion on .dat file ?
Here is the sample value, it has headers DF_PARTY_ID;DF_PARTY_CODE;DF_CONNECTION_ID and its value as mentioned under.
Sample Data:
DF_PARTY_ID;DF_PARTY_CODE;DF_CONNECTION_ID
87961526;4002524;13575326
87966204;4007202;13564782
What's wrong with fgetcsv()? The extension on the file is irrelevant as long as the format of the data is consistent across all of your files.
Example:
$fh = fopen('example.dat', 'r');
while (!feof($fh)) {
var_dump(fgetcsv($fh, 0, ';'));
}
Alternatively, with PHP5.3 you can also do:
$lines = file('example.dat');
foreach($lines as $line) {
var_dump(str_getcsv(trim($line), 0, ';'));
}
IMHO .dat files can be of different formats. Blindly following the extension can be error-prone. If however you have a file from some specific application, maybe tell us what this app is. Chances are there are some parsing libraries or routines.
I would imagine it would be easier to write a short function using fopen, fread, and fclose to parse it yourself. Read each line, explode to an array, and store them as you wish.

Categories