I'm importing a .CSV into an application with the following:
if (($handle = fopen($file, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
}
fclose($handle);
}
It sort of works but it's far from perfect. I wanted to first get out the row heads and put them into an array and then loop round each row to get the data sets in.
It seems to be having delimitting problems as the first row (heads) are also including a few parts of the second row.
I exported the .csv file straight from Excel. Wonder if there are encoding tricks etc I might be missing.
It sounds like your 1000 limit is not long enough; you should set it to a high enough value for that file ... or set to 0 for unlimited (not recommended, because it tends to be slower).
Set it to 4096 or 8192 first and see how it goes.
// use 8kB buffer for reading comma delimited line
while (($data = fgetcsv($handle, 8192, ",")) !== FALSE) {
Update
Okay, on second thought, perhaps you should inspect the file and confirm a few things:
Are the delimiters really a comma? (I guess you've already established this)
Are the string enclosures always double quotes?
Are strings escaped with a backslash (default) or another double quote?
fgetcsv doesnt always properly detect line endings.
Try using before the fgetcsv call:
ini_set('auto_detect_line_endings', true);
I changed to 8192 and got same results
while (($data = fgetcsv($handle, 8192, ",")) !== FALSE) {
...
}
and then did this one and got same results
$handle = fopen($_FILES['filename']['tmp_name'], "r");
ini_set('auto_detect_line_endings', true);
while (($data = fgetcsv($handle, 8192, ",")) !== FALSE) {
$data = array_map('mysql_real_escape_string', $data);
Why are you not using file()?
$trimmed = file('somefile.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
Related
I have CSV file with special characters ØÅÆ
When I use fgetcsv, it just ignores the rest of the string. I tried var_dump for the row, but I got it like this: ���
This is the code that I have:
if (($handle = fopen($path, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 0, ",")) !== FALSE) {
var_dump($data); die;
}
fclose($handle);
}
I tried adding this before my code setlocale(LC_ALL, 'en_US.UTF-8');
but without any luck. Do you have any advice?
Some sample data - first row from CSV:
VAR,E,5704617332886,500,11,"TURKISøåæØÅÆ","110"
The proper encoding should be ISO-8859-1. That was the problem
I'm currently opening a TAB delimited file with the following code...
if (($handle = fopen($filetxt, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 0, "\t")) !== FALSE) {
// var_dump($data);
$num = count($data);
echo "<br>\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
}
fclose($handle);
}
Now I'm just trying to figure out how I can put the data into a CSV using fputcsv. An example would be great -- right now I'm just trying to get the headers into a CSV and then I can go from there.
The data right now (that's being echoed) looked like this...
part_number
aaia_part_term_id
short_description
bullet_points
list_price
jobber_price
base_price
epc_code
length
width
height
weight
image_name
If I could just figure out how to echo only the header into the CSV, I think I can figure out the rest.
fopen only gives you a handle to the file. A csv file's formatting relies going through each line, the way you are reading the file uses all contents at once, try:
<?PHP
if($handle = fopen($filetext, "r") !== FALSE){
while (!feof($handle)) {
$line_of_text = fgets($handle);
foreach( fgetcsv($line_of_text, 0, "\t") as $csv_item)
print $csv_item."," ;
print "<br>";
}
fclose($handle);
}
?>
I haven't tested this
Since a CSV file is just a comma delimited data file, it might be simpler just to read the current tab-delimited file line by line, and replace tabs with commas
if (($handle = #fopen($filetxt, "r")) !== false) {
while (($line = fgets($handle, 0)) !== false) {
$csv_line = str_replace("\t", ",", $line);
// write $csv_line to your csv file
}
fclose($handle);
}
I have a CSV file and I read data from CSV file then I want to skip first line of CSV file.Which'll contain any header. I am using this code.
while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE)
{
// Code to insert into database
}
When I insert data into th database then header should not to be saved into the database.
Before beginning the while loop, just get the first line and do nothing with it. This way the logic to test if it's the first line is not needed.
fgetcsv($file, 10000, ",");
while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE) {
//....
}
try:
$flag = true;
while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE) {
if($flag) { $flag = false; continue; }
// rest of your code
}
A bit late, but here is another way to do so (without having to count all the lines): with fgets
$file = fopen($filename, 'r'); // create handler
fgets($file); // read one line for nothing (skip header)
while (($line = fgetcsv($file, 10000, ",")) !== FALSE) {
// do your thing
}
One might consider this to be more elegant
You can add a simple check and skip the query if the check fails:
$firstline = true;
while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE)
{
if (!$firstline) {
// Code to insert into database
}
$firstline = false;
}
Try this simple code.
$file = fopen('example.csv', 'r'); // Here example is a CSV name
$row = 1;
while (($line = fgetcsv($file, 10000, ",")) !== FALSE) {
// $line is an array of the csv elements
if($row == 1){ $row++; continue; } // continue is used for skip row 1
// print_r($line);
// rest of your code
}
You should use the fseek() method in order to get the desired line, regardless the current pointer position.
At this example, get the first line after the loop:
$file = fopen($path, "r");
while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE) {
// ...
}
fseek($file, 1, SEEK_CUR);
You can use the third parameter to position the pointer in the file, as is:
SEEK_SET – It moves file pointer position to the beginning of the file.
SEEK_CUR – It moves file pointer position to given location.
SEEK_END – It moves file pointer position to the end of file.
Hi I have a fgetcsv function that reads a CSV file and exports the data into a list. This works, and I have used this function numerous times elsewhere with no problems.
The relevant bits of the code are:
ini_set("auto_detect_line_endings", true);
$file = fopen($_FILES['filename']['tmp_name'],"r");
while (($data = fgetcsv($file, 1000, ",")) !== FALSE)
{
$body_data['user_list'][] = $data;
}
fclose($file);
The problem is that the read CSV stops at any spaces (white space) between words in columned data.
I thought that auto_detect_line_endings would solve this, could it be that this is causing the problem?
Change your while loop with this
while (!feof($file) ) {
$body_data['user_list'][] = fgetcsv($file, 1024);
}
Is it possible to use fgetcsv in PHP to open a tab-delimited file?
$csvData = fgetcsv($fileHandle, 0, "\t");
Where $fileHandle is a valid file handle. The 0 is just to tell the function not to limit seeking through lines (however you can change this to suit, the docs do say not imposing a limit decreases performance).
Make sure to use double quotes around the "\t", single quotes will not work.
$fh = fopen($file, 'r');
while (($line = fgetcsv($fh, 0, "\t")) !== false) {
// do stuff
}
yes, you can specify tab "\t" in its parameters. see the doc.
while (($data = fgetcsv($handle, 1000, "\t")) !== FALSE)