import.lib.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. // $Id: import.lib.php 13806 2007-11-28 06:29:03Z yannoo $
  3. /*
  4. ==============================================================================
  5. Dokeos - elearning and course management software
  6. Copyright (c) 2004,2005 Dokeos S.A.
  7. Copyright (c) Bart Mollet, Hogeschool Gent
  8. For a full list of contributors, see "credits.txt".
  9. The full license can be read in "license.txt".
  10. This program is free software; you can redistribute it and/or
  11. modify it under the terms of the GNU General Public License
  12. as published by the Free Software Foundation; either version 2
  13. of the License, or (at your option) any later version.
  14. See the GNU General Public License for more details.
  15. Contact address: Dokeos, 44 rue des palais, B-1030 Brussels, Belgium
  16. Mail: info@dokeos.com
  17. ==============================================================================
  18. */
  19. /**
  20. ==============================================================================
  21. * This class provides some functions which can be used when importing data from
  22. * external files into Dokeos
  23. * @package dokeos.library
  24. ==============================================================================
  25. */
  26. class Import
  27. {
  28. /**
  29. * Reads a CSV-file into an array. The first line of the CSV-file should
  30. * contain the array-keys.
  31. * Example:
  32. * FirstName;LastName;Email
  33. * John;Doe;john.doe@mail.com
  34. * Adam;Adams;adam@mail.com
  35. * returns
  36. * $result [0]['FirstName'] = 'John';
  37. * $result [0]['LastName'] = 'Doe';
  38. * $result [0]['Email'] = 'john.doe@mail. com';
  39. * $result [1]['FirstName'] = 'Adam';
  40. * ...
  41. * @param string $filename Path to the CSV-file which should be imported
  42. * @return array An array with all data from the CSV-file
  43. */
  44. function csv_to_array($filename) {
  45. $result = array ();
  46. $handle = fopen($filename, "r");
  47. if($handle === false) {
  48. return $result;
  49. }
  50. $keys = fgetcsv($handle, 4096, ";");
  51. while (($row_tmp = fgetcsv($handle, 4096, ";")) !== FALSE) {
  52. $row = array ();
  53. //avoid empty lines in csv
  54. if (is_array($row_tmp) && count($row_tmp)>0 && $row_tmp[0]!= '') {
  55. if (!is_null($row_tmp[0])) {
  56. foreach ($row_tmp as $index => $value) {
  57. $row[$keys[$index]] = $value;
  58. }
  59. $result[] = $row;
  60. }
  61. }
  62. }
  63. fclose($handle);
  64. return $result;
  65. }
  66. }
  67. ?>