Skip to content

Instantly share code, notes, and snippets.

@Macagare
Created October 29, 2012 08:23
Show Gist options
  • Save Macagare/3972324 to your computer and use it in GitHub Desktop.
Save Macagare/3972324 to your computer and use it in GitHub Desktop.
PHP: Open and read a csv file via PHP
<?php
$fh = fopen("myfile.csv", "r");
while ($line = fgets($fh)) { // Read one line at-a-time until end of file
$words = explode (';', $line ); // break the line down to words, by separating on a ';'
echo "This line consisted of the following words:\n";
foreach ($words as $word) { //display all words
echo "Word: ". $word. "\n";
}
// update the database here.
}
fclose($fh);
?>
<?php
$fh = fopen("myfile.csv", "r");
while ($line = fgets($fh)) { // Read one line at-a-time until end of file
$words = explode (';', $line ); // break the line down to words, by separating on a ';'
echo "This line consisted of the following words:\n";
foreach ($words as $word) { //display all words
echo "Word: ". $word. "\n";
}
// update the database here.
}
fclose($fh);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment