I have the following csv file:
upc/ean/isbn,item name,category,supplier id,cost price,unit price,tax 1 name,tax 1 percent,tax 2 name,tax 2 percent,quantity,reorder level,description,allow alt. description,item has serial number
,Apple iMac,Computers,,10,12,8,8,10,10,10,1,Best computer,,
,Apple iMac,Computers,,10,12,8,8,10,10,10,1,Best computer,,
,Apple iMac,Computers,,10,12,8,8,10,10,10,1,Best computer,,
,Apple iMac,Computers,,10,12,8,8,10,10,10,1,Best computer,,
,Apple iMac,Computers,,10,12,8,8,10,10,10,1,Best computer,,
,Apple iMac,Computers,,10,12,8,8,10,10,10,1,Best computer,,
,Apple iMac,Computers,,10,12,8,8,10,10,10,1,Best computer,,
,Apple iMac,Computers,,10,12,8,8,10,10,10,1,Best computer,,
,Apple iMac,Computers,,10,12,8,8,10,10,10,1,Best computer,,
,Apple iMac,Computers,,10,12,8,8,10,10,10,1,Best computer,,
,Apple iMac,Computers,,10,12,8,8,10,10,10,1,Best computer,,
,Apple iMac,Computers,,10,12,8,8,10,10,10,1,Best computer,,
When I do this:
if (($handle = fopen($_FILES['file_path']['tmp_name'], "r")) !== FALSE)
{
$data = fgetcsv($handle);
var_dump($data);
}
I get an array with 183 elements, I would expect to only get one line of the csv, but I get the whole file.
I even tried $data = fgetcsv($handle, 1000);
and I got the same result
PHP fgetcsv() Function$file = fopen("contacts. csv","r"); print_r(fgetcsv($file)); fclose($file);
You can open the file using fopen() as usual, get each line by using fgets() and then simply explode it on each comma like this: <? php $handle = @fopen("/tmp/inputfile. txt", "r"); if ($handle) { while (($buffer = fgets($handle)) !==
This is most likely a line ending issue. Try enabling auto_detect_line_endings
which will attempt to determine the file's line endings.
ini_set('auto_detect_line_endings', true);
If that doesn't resolve the issue, then detect the type of line terminators using the file
command:
$ file example.csv example.csv: ASCII text, with CR line terminators
You can then convert the line endings. I am not sure what OS you are using but there are a lot of utilities out there for file format conversion, e.g. dos2unix
.
This is the shortest solution I could think of:
$fp = fopen('test.csv', 'r');
// get the first (header) line
$header = fgetcsv($fp);
// get the rest of the rows
$data = array();
while ($row = fgetcsv($fp)) {
$arr = array();
foreach ($header as $i => $col)
$arr[$col] = $row[$i];
$data[] = $arr;
}
print_r($data);
Output:
Array
(
[0] => Array
(
[upc/ean/isbn] =>
[item name] => Apple iMac
[category] => Computers
[supplier id] =>
[cost price] => 10
[unit price] => 12
[tax 1 name] => 8
[tax 1 percent] => 8
[tax 2 name] => 10
[tax 2 percent] => 10
[quantity] => 10
[reorder level] => 1
[description] => Best computer
[allow alt. description] =>
[item has serial number] =>
)
[1] => Array
(
[upc/ean/isbn] =>
[item name] => Apple iMac
[category] => Computers
[supplier id] =>
[cost price] => 10
[unit price] => 12
[tax 1 name] => 8
[tax 1 percent] => 8
[tax 2 name] => 10
[tax 2 percent] => 10
[quantity] => 10
[reorder level] => 1
[description] => Best computer
[allow alt. description] =>
[item has serial number] =>
)
// ...
[11] => Array
(
[upc/ean/isbn] =>
[item name] => Apple iMac
[category] => Computers
[supplier id] =>
[cost price] => 10
[unit price] => 12
[tax 1 name] => 8
[tax 1 percent] => 8
[tax 2 name] => 10
[tax 2 percent] => 10
[quantity] => 10
[reorder level] => 1
[description] => Best computer
[allow alt. description] =>
[item has serial number] =>
)
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With