国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home Backend Development PHP Tutorial PHP classes for text file operations

PHP classes for text file operations

Jul 25, 2016 am 08:43 AM

  1. var $file;
  2. var $index;
  3. //Create a file and write input
  4. function null_write($new) {
  5. $f=fopen($this-> file,"w");
  6. flock($f,LOCK_EX);
  7. fputs($f,$new);
  8. fclose($f);
  9. }
  10. // Add data records to the end of the file
  11. function add_write($new ) {
  12. $f=fopen($this->file,"a");
  13. flock($f,LOCK_EX);
  14. fputs($f,$new);
  15. fclose($f);
  16. }
  17. / / Used with the return of readfile() to convert a row of data into a one-dimensional array
  18. function make_array($line) {
  19. $array = explode("\x0E",$line);
  20. return $array;
  21. }
  22. / /Convert one row of data into a one-dimensional array
  23. function join_array($line) {
  24. $array = join("\x0E",$line); return $array;
  25. }
  26. // Return the total number of lines in the data file
  27. function getlines () {
  28. $f=file($this->file);
  29. return count($f);
  30. }
  31. // Return the data record of the next line (backup)
  32. function next_line() {
  33. $this-> ;index=$this->index++;
  34. return $this->get();
  35. }
  36. // Return the data record of the previous line (backup)
  37. function prev_line() {
  38. $this->index=$ this->index--;
  39. return $this->get();
  40. }
  41. // Return the data record of the current row. The data is small
  42. function get() {
  43. $f=fopen($this-> file,"r");
  44. flock($f,LOCK_SH);
  45. for($i=0;$i<=$this->index;$i++) {
  46. $rec=fgets($f,1024) ;
  47. }
  48. $line=explode("\x0E",$rec);
  49. fclose($f);
  50. return $line;
  51. }
  52. // Return the data record of the current line. The data is larger
  53. function get_big_file() {
  54. $f=fopen($this->file,"r");
  55. flock($f,LOCK_SH);
  56. for($i=0;$i<=$this->index;$i++) {
  57. $rec=fgets($f,1024*5);
  58. }
  59. $line=explode("\x0E",$rec);
  60. fclose($f);
  61. return $line;
  62. }
  63. // Open data File --- Return the file content as a one-dimensional array
  64. function read_file() {
  65. if (file_exists($this->file)) {
  66. $line =file($this->file);
  67. }
  68. return $ line;
  69. }
  70. // Open the data file --- return the file content as a two-dimensional array
  71. function openFile() {
  72. if (file_exists($this->file)) {
  73. $f =file($this-> ;file);
  74. $lines = array();
  75. foreach ($f as $rawline) {
  76. $tmpline = explode("\x0E",$rawline);
  77. array_push($lines, $tmpline);
  78. }
  79. }
  80. return $lines;
  81. }
  82. // Pass in an array, merge it into one line of data, and rewrite the entire file
  83. function overwrite($array){
  84. $newline = implode("\x0E",$array);
  85. $ f = fopen($this->file,"w");
  86. flock($f,LOCK_EX);
  87. fputs($f,$newline);
  88. fclose($f);
  89. }
  90. // Add a row of data Record to the end of the file
  91. function add_line($array,$check_n=1) {
  92. $s=implode("\x0E",$array);
  93. $f=fopen($this->file,"a");
  94. flock($f,LOCK_EX);
  95. fputs($f,$s);
  96. if ($check_n==1)
  97. fputs($f,"\n");
  98. fclose($f);
  99. }
  100. // Insert a row of data records to the front of the file
  101. function insert_line($array) {
  102. $newfile = implode("\x0E",$array);
  103. $f = fopen($this->file,"r") ;
  104. flock($f,LOCK_SH);
  105. while ($line = fgets($f,1024)) {
  106. $newfile .= $line;
  107. }
  108. fclose($f);
  109. $f = fopen($this ->file,"w");
  110. flock($f,LOCK_EX);
  111. fputs($f,$newfile);
  112. fclose($f);
  113. }
  114. // Update all qualified data records, suitable for situations where the byte data in each row is large
  115. function update($column,$query_string,$update_array) {
  116. $update_string = implode("\x0E",$update_array) ;
  117. $newfile = "";
  118. $fc=file($this->file);
  119. $f=fopen($this->file,"r");
  120. flock($f,LOCK_SH);
  121. for ($i=0;$i $list = explode("\x0E",$fc[$i]);
  122. if ($list[$column] != $query_string) {
  123. $newfile = $newfile.chop($fc[$i])."\n";
  124. } else {
  125. $newfile = $newfile.$update_string;
  126. }
  127. }
  128. fclose($f) ;
  129. $f=fopen($this->file,"w");
  130. flock($f,LOCK_EX);
  131. fputs($f,$newfile);
  132. fclose($f);
  133. }
  134. // Update all qualified data records, suitable for cases where the byte data in each row is small
  135. function update2($column,$query_string,$update_array) {
  136. $newline = implode("\x0E",$update_array);
  137. $ newfile = "";
  138. $f = fopen($this->file,"r");
  139. flock($f,LOCK_SH);
  140. while ($line = fgets($f,1024)) {
  141. $tmpLine = explode("\x0E",$line);
  142. if ($tmpLine[$column] == $query_string) {
  143. $newfile .= $newline;
  144. } else {
  145. $newfile .= $line;
  146. }
  147. }
  148. fclose($f);
  149. $f = fopen($this->file,"w");
  150. flock($f,LOCK_EX);
  151. fputs($f,$newfile);
  152. fclose($f ; $this->file);
  153. $f=fopen($this->file,"r");
  154. flock($f,LOCK_SH);
  155. for ($i=0;$i $list = explode("\x0E",$fc[$i]);
  156. if ($list[$column] != $query_string) {
  157. $newfile = $newfile.chop($ fc[$i])."\n";
  158. }
  159. }
  160. fclose($f);
  161. $f=fopen($this->file,"w");
  162. flock($f,LOCK_EX);
  163. fputs($f,$newfile);
  164. fclose($f);
  165. }
  166. // Delete all qualified data records, suitable for cases where the byte data per row is small
  167. function delete2($column,$query_string ){
  168. $newfile = "";
  169. $f = fopen($this->file,"r");
  170. flock($f,LOCK_SH);
  171. while ($line = fgets($f,1024)) {
  172. $tmpLine = explode("\x0E",$line);
  173. if ($tmpLine[$column] != $query_string) {
  174. $newfile .= $line;
  175. }
  176. }
  177. fclose($f);
  178. $f = fopen($this->file,"w");
  179. flock($f,LOCK_EX);
  180. fputs($f,$newfile);
  181. fclose($f);
  182. }
  183. //Get The maximum value of a field in a file
  184. function get_max_value($column) {
  185. $tlines = file($this->file);
  186. for ($i=0;$i<=count($tlines);$ i++) {
  187. $line=explode("\x0E",$tlines[$i]);
  188. $get_value[]=$line[$column];
  189. }
  190. $get_max_value = max($get_value);
  191. return $ get_max_value;
  192. }
  193. // Query based on whether a field in the data file contains $query_string, and return all qualified data in a two-dimensional array
  194. function select($column, $query_string) {
  195. $tline = $this-> ;openfile();
  196. $lines = array();
  197. foreach ($tline as $line) {
  198. if ($line[$column] == $query_string) {
  199. array_push($lines, $line);
  200. }
  201. }
  202. return $lines;
  203. }
  204. // The function is the same as function select(), the speed may be slightly improved
  205. function select2($column, $query_string) {
  206. if (file_exists($this->file)) {
  207. $tline = $this-> read_file();
  208. foreach ($tline as $tmpLine) {
  209. $line = $this->make_array($tmpLine);
  210. if ($line[$column] == $query_string) {
  211. $lines[]= $tmpLine;
  212. }
  213. }
  214. }
  215. return $lines;
  216. }
  217. // Query based on whether a field in the data file contains $query_string, and return the first qualified data in a one-dimensional array
  218. function select_line($ column, $query_string) {
  219. $tline = $this->read_file();
  220. foreach ($tline as $tmpLine) {
  221. $line = $this->make_array($tmpLine);
  222. if ($line[ $column] == $query_string) {
  223. return $line;
  224. break;
  225. }
  226. }
  227. }
  228. // select next/prev line(next_prev ==> 1/next, 2/prev) by cx
  229. function select_next_prev_line ($column, $query_string, $next_prev) {
  230. $tline = $this->read_file();
  231. $line_key_end = count($tline) - 1;
  232. $line_key = -1;
  233. foreach ($tline as $ tmpLine) {
  234. $line_key++;
  235. $line = $this->make_array($tmpLine);
  236. if ($next_prev == 1) {
  237. // next?
  238. if ($line[$column] == $query_string ) {
  239. if ($line_key == 0) {
  240. return 0;
  241. } else {
  242. $line_key_up = $line_key - 1;
  243. return $up_line;
  244. }
  245. } else {
  246. $up_line = $line;
  247. }
  248. } elseif ($next_prev == 2) {
  249. // prev?
  250. if ($line[$column] == $query_string) {
  251. if ($line_key == $line_key_end) {
  252. return 0;
  253. } else {
  254. $line_key_down = $line_key + 1;
  255. break;
  256. }
  257. }
  258. } else {
  259. return 0;
  260. }
  261. }
  262. $down_line = $this->make_array($tline[$line_key_down]);
  263. return $down_line ;
  264. }
  265. ?>
Copy code

Text file, php


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are some best practices for versioning a PHP-based API? What are some best practices for versioning a PHP-based API? Jun 14, 2025 am 12:27 AM

ToversionaPHP-basedAPIeffectively,useURL-basedversioningforclarityandeaseofrouting,separateversionedcodetoavoidconflicts,deprecateoldversionswithclearcommunication,andconsidercustomheadersonlywhennecessary.StartbyplacingtheversionintheURL(e.g.,/api/v

How do I implement authentication and authorization in PHP? How do I implement authentication and authorization in PHP? Jun 20, 2025 am 01:03 AM

TosecurelyhandleauthenticationandauthorizationinPHP,followthesesteps:1.Alwayshashpasswordswithpassword_hash()andverifyusingpassword_verify(),usepreparedstatementstopreventSQLinjection,andstoreuserdatain$_SESSIONafterlogin.2.Implementrole-basedaccessc

What are the differences between procedural and object-oriented programming paradigms in PHP? What are the differences between procedural and object-oriented programming paradigms in PHP? Jun 14, 2025 am 12:25 AM

Proceduralandobject-orientedprogramming(OOP)inPHPdiffersignificantlyinstructure,reusability,anddatahandling.1.Proceduralprogrammingusesfunctionsorganizedsequentially,suitableforsmallscripts.2.OOPorganizescodeintoclassesandobjects,modelingreal-worlden

What are weak references (WeakMap) in PHP, and when might they be useful? What are weak references (WeakMap) in PHP, and when might they be useful? Jun 14, 2025 am 12:25 AM

PHPdoesnothaveabuilt-inWeakMapbutoffersWeakReferenceforsimilarfunctionality.1.WeakReferenceallowsholdingreferenceswithoutpreventinggarbagecollection.2.Itisusefulforcaching,eventlisteners,andmetadatawithoutaffectingobjectlifecycles.3.YoucansimulateaWe

How can you handle file uploads securely in PHP? How can you handle file uploads securely in PHP? Jun 19, 2025 am 01:05 AM

To safely handle file uploads in PHP, the core is to verify file types, rename files, and restrict permissions. 1. Use finfo_file() to check the real MIME type, and only specific types such as image/jpeg are allowed; 2. Use uniqid() to generate random file names and store them in non-Web root directory; 3. Limit file size through php.ini and HTML forms, and set directory permissions to 0755; 4. Use ClamAV to scan malware to enhance security. These steps effectively prevent security vulnerabilities and ensure that the file upload process is safe and reliable.

How can you interact with NoSQL databases (e.g., MongoDB, Redis) from PHP? How can you interact with NoSQL databases (e.g., MongoDB, Redis) from PHP? Jun 19, 2025 am 01:07 AM

Yes, PHP can interact with NoSQL databases like MongoDB and Redis through specific extensions or libraries. First, use the MongoDBPHP driver (installed through PECL or Composer) to create client instances and operate databases and collections, supporting insertion, query, aggregation and other operations; second, use the Predis library or phpredis extension to connect to Redis, perform key-value settings and acquisitions, and recommend phpredis for high-performance scenarios, while Predis is convenient for rapid deployment; both are suitable for production environments and are well-documented.

What are the differences between == (loose comparison) and === (strict comparison) in PHP? What are the differences between == (loose comparison) and === (strict comparison) in PHP? Jun 19, 2025 am 01:07 AM

In PHP, the main difference between == and == is the strictness of type checking. ==Type conversion will be performed before comparison, for example, 5=="5" returns true, and ===Request that the value and type are the same before true will be returned, for example, 5==="5" returns false. In usage scenarios, === is more secure and should be used first, and == is only used when type conversion is required.

How do I perform arithmetic operations in PHP ( , -, *, /, %)? How do I perform arithmetic operations in PHP ( , -, *, /, %)? Jun 19, 2025 pm 05:13 PM

The methods of using basic mathematical operations in PHP are as follows: 1. Addition signs support integers and floating-point numbers, and can also be used for variables. String numbers will be automatically converted but not recommended to dependencies; 2. Subtraction signs use - signs, variables are the same, and type conversion is also applicable; 3. Multiplication signs use * signs, which are suitable for numbers and similar strings; 4. Division uses / signs, which need to avoid dividing by zero, and note that the result may be floating-point numbers; 5. Taking the modulus signs can be used to judge odd and even numbers, and when processing negative numbers, the remainder signs are consistent with the dividend. The key to using these operators correctly is to ensure that the data types are clear and the boundary situation is handled well.

See all articles