import.lib.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. {
  46. $result = array ();
  47. $handle = fopen($filename, "r");
  48. if($handle === false)
  49. {
  50. return $result;
  51. }
  52. $keys = fgetcsv($handle, 1000, ";");
  53. while (($row_tmp = fgetcsv($handle, 1000, ";")) !== FALSE)
  54. {
  55. $row = array ();
  56. foreach ($row_tmp as $index => $value)
  57. {
  58. $row[$keys[$index]] = $value;
  59. }
  60. $result[] = $row;
  61. }
  62. fclose($handle);
  63. return $result;
  64. }
  65. }
  66. ?>