Read and write data to textfile


/ Published in: PHP
Save to your folder(s)

I created this code for practice, and I looking for better and clear solution for the last instance where I can write or delete the data, but after I want to get back the content as array or the key as string. I want to use this write-read back with only one instance.


Copy this code and paste it in your HTML
  1. // The input is a textfile:
  2. // key1=val1
  3. // key2=val2
  4. // key3=val3
  5. // key4=val4
  6. // .
  7. // .
  8. // .
  9.  
  10. // The class:
  11.  
  12. class UserID {
  13. private $UIDPath;
  14. private $UserUIDcontent;
  15.  
  16. public function __construct($UserID, $Action, $UID_KEY, $UID_VAL, $Back) {
  17. $UID_Content = $this -> UIDPath = 'the/path/to/input/text.txt';
  18. $UserUIDcontent = array();
  19.  
  20. if (file_exists($UID_Content) && (is_file($UID_Content))) {
  21. foreach (file($UID_Content) as $UIDlines) {
  22. list($key, $value) = explode('=', $UIDlines, 2) + array(NULL, NULL);
  23. if (trim($value) !== NULL) { $UserUIDcontent[$key] = trim($value); }
  24. }
  25.  
  26. if ($Action == 'Read') {
  27. if ($UID_KEY == NULL) {
  28. $this -> UserUIDcontent = $UserUIDcontent;
  29. } else {
  30. if (array_key_exists($UID_KEY, $UserUIDcontent)) {
  31. $this -> UserUIDcontent = $UserUIDcontent[$UID_KEY];
  32. } else {
  33. if ($UID_KEY == 'User') {
  34. return NULL;
  35. } else {
  36. unset($UserUIDcontent[$UID_KEY]);
  37. echo 'ERROR: The search key: ' . $UID_KEY . ' not available in Class: ' . __METHOD__;
  38. return;
  39. }
  40. }
  41. }
  42. } elseif ($Action == 'Write') {
  43. if (is_array($UID_KEY)) {
  44. if (count($UID_KEY) == count($UID_VAL)) {
  45. $UserUIDcontent = array_filter(array_merge($UserUIDcontent, array_combine($UID_KEY, $UID_VAL)));
  46. } else {
  47. echo 'ERROR: Keys: ' . count($UID_KEY) . ' and Values: ' . count($UID_VAL) . ' are not equal in Class: ' . __METHOD__;
  48. return;
  49. }
  50. } else {
  51. $UserUIDcontent[$UID_KEY] = $UID_VAL;
  52. }
  53. foreach ($UserUIDcontent as $key => $value) {
  54. if ($value !== NULL) {
  55. $UIDcontentTXT .= trim($key) . '=' . trim($value) . PHP_EOL;
  56. }
  57. }
  58. trim($UIDcontentTXT);
  59. $handle = fopen($UID_Content, "w");
  60. fwrite($handle, $UIDcontentTXT, strlen($UIDcontentTXT));
  61. fclose($handle);
  62.  
  63. if ($Back != NULL) {
  64. if (array_key_exists($Back, $UserUIDcontent)) {
  65. return $this -> $UserUIDcontent = $UserUIDcontent[$Back];
  66. } else {
  67. return $this -> $UserUIDcontent = $UserUIDcontent;
  68. }
  69. } else {
  70. return $this -> $UserUIDcontent = NULL;
  71. }
  72. } else {
  73. echo 'ERROR: The second parameter ' . $Action . ' not allowed in Class: ' . __METHOD__;
  74. return;
  75. }
  76. } else {
  77. echo 'ERROR: Required data source not found for Class: ' . __METHOD__;
  78. return;
  79. }
  80. }
  81.  
  82. public function result() {
  83. return $this -> UserUIDcontent;
  84. }
  85. }
  86.  
  87. // ---------------------------------
  88. // instances:
  89. // ---------------------------------
  90.  
  91. $UserData = new UserID($UserID, 'Read', NULL, NULL, NULL);
  92. $array = $UserData -> result(); // Read full array
  93. $val = $UserData -> result()[key2]; // Read one data
  94.  
  95. $UserData = new UserID($UserID, 'Read', 'key2', NULL, NULL);
  96. $val = $UserData -> result(); // Read one data
  97.  
  98. $UserData = new UserID($UserID, 'Write', array('key2', 'aaaaaaaaa', 'User'), array(NULL, 'bbbbbbbb', NULL), NULL);
  99. $UserData; // write or delete more than one data by array
  100.  
  101. $UserData = new UserID($UserID, 'Write', 'key2', NULL, NULL);
  102. $UserData; // write or delete one data
  103.  
  104. // this is the problem:
  105. $UserData = new UserID($UserID, 'Write', array('User', 'key2', 'key3', 'key15'), array('aaaa', NULL, 'aaa', 'value15'), 1);
  106. $array = $UserData -> Array; // Write AND Read back full array or one key

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.