Php operations on files (modify/delete)

Source: Internet
Author: User
I forgot to write this on the right. By the way, many of my friends used the dat file to store small-capacity data and build a text data table, many friends, like me, are very familiar with how to append, delete, and clear files. However, they are helpless for a certain record operation in the file, I recently wrote a small message book to implement this function. it seems that I forgot to write it on the right. By the way, I will write it again.


Many friends use dat files to store small-capacity data and build text data tables. many friends, like me, are very familiar with how to append, delete, and clear files, however, I am helpless with a certain record operation in the file. recently I wrote a small message book to implement this function, so that I can explain it to you.

Example:
Data. dat
_________________________________________________________

[1] name your sex phone subject address your email subject

_________________________________________________________
This file is the file that saves data.
[1] ------------- indicates record data
Partition ------------- indicates the delimiter of a data table. it can be a non-\ r, \ n character.

Read the page read. php
(Content omitted)
Orchestrate and display data in data. dat. As follows:

1. How much do you pull --- published by (sports98) 2002-13-42 □( checkbox)
2. how much do you pull --- published by (sports98) 2002-13-42 □( checkbox)
3. how much do you pull-published by (sports98) 2002-13-42 □( checkbox)
4. How much do you pull-published by (sports98) 2002-13-42 □( checkbox)
Upload checkbox information through form
Do. php


Do. php contains the following content:

$ Filename = "data. dat"; // data file
$ Fp = file ($ filename); // read data files
$ Number = count ($ fp); // calculates the total data volume
For ($ int_a = 0; $ int_a <$ number; $ int_a ++ ){
If ($ int_a = $ checkbox []) // here can be one or multiple judgments. Here we take deleting a record as an example.
Continue;
Else
$ Newmem. = $ fp [$ int_a]. "\ r"; // add file content
}
$ Wfp = fopen ($ filename, "w ");
Fputs ($ wfp, $ newmen );
Fclose ($ wfp );
The idea of deleting is to read the data in the text into the memory, clear the file write after the operation is completed in the memory, and skip the records that meet the deletion conditions to delete them,
Editing is to replace the matching record to pull ~



$ Filename = "data. dat"; // data file
$ Fp = file ($ filename); // read data files
$ Number = count ($ fp); // calculates the total data volume
For ($ int_a = 0; $ int_a <$ number; $ int_a ++ ){
If ($ int_a = $ checkbox []) // here can be one or multiple judgments. Here we take editing a record as an example.
$ Newmem. = ""; // Here is the modified content
Else
$ Newmem. = $ fp [$ int_a]. "\ r"; // add file content
}
$ Wfp = fopen ($ filename, "w ");
Fputs ($ wfp, $ newmen );
Fclose ($ wfp );
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.