PHP fopen function read and write txt file Implementation Program

Source: Internet
Author: User

Below we use several examples to summarize the use of the php fopen function to implement file read and write operations. If you need to learn, refer.

Simple reference to fopen Functions

The fopen () function opens a file or URL.

If opening fails, this function returns FALSE.

Syntax
Fopen (filename, mode, include_path, context)

Instance 1

Example of creating a file:

The Code is as follows: Copy code

<? Php
If (! File_exists ("test.txt") {// if the file does not exist (the default value is in the current directory)
$ Fh = fopen ("test.txt", "w ");
Fclose ($ fh); // close the file
}
?>


Instance 2

Modify and edit a robots file using php's ability to read and write text documents

The Code is as follows: Copy code

<? Php
Function get_txt ($ robots_file)
// Define the function. The content is included in {}.
{
If (file_exists ($ robots_file ))
// If the object exists, read the content.
{
$ Fp = @ fopen ($ robots_file, "r ");
// R is the abbreviation of read, which indicates reading. Open a file in read-only mode.
If ($ fp ){
While (! Feof ($ fp) {// if it is not at the end of the file
$ Robots_txt = fgets ($ fp, 4096); // read data row by row
$ Robots_all = $ robots_all. $ robots_txt; // Save the data to $ robots_all.
}
Return ($ robots_all); // return all content
Fclose ($ fp); // close the file
}
}
}

Function put_txt ($ robots_txt)
{
$ Fp = fopen ("robots.txt", "w ");
// W is the abbreviation of write, which indicates the meaning of writing. open the file as a write.
Fputs ($ fp, $ robots_txt );
// Output text to a file
Fclose ($ fp );
}
?>

<? Php
$ Edit = $ _ GET ["edit"];
$ Txt = $ _ POST ["txt"];
$ Get_txt = get_txt ("robots.txt ");
// Call the defined function to open the robots file.

If ($ edit = "write ")
{
Put_txt ($ txt );
Echo "successfully saved <a href = robots-editer.php> back </a> ";
}
Else
{
Echo "<a hrefw.robots.txt target = _ blank> robots.txt </a> <a href = writer. php> returned </a> ";
}
?>

<? Php
If ($ edit = "")
{
?>
<Form name = "form1" action = "? Edit = write "method =" post ">
<Textarea name = "txt" cols = "160" rows = "30"> <? Php echo $ get_txt;?> </Textarea>
<Br/>
<Input name = "submit" value = "save" type = "submit"/>
</Form>
<? Php
}
?>

Read the data in the counter.txt file through PHP and save it to the text document with + 1.

Create a counter. php document and enter the following code. Unlike ASP, single-line comments in PHP are implemented with // or #, and multi-line comments are implemented:

 

The Code is as follows: Copy code
<? Php
Function get_hit ($ counter_file)
// Define the function. The content is included in {}. It should be similar to the C language.
{
$ Count = 0;
// Returns the counter to zero. Add the $ sign before the variable in Php.

If (file_exists ($ counter_file ))
// If the counter file exists, read the content
{
$ Fp = fopen ($ counter_file, "r ");
// R is the abbreviation of read, which indicates reading. Open a file in read-only mode.
$ Count = 0 + fgets ($ fp, 20 );
/* Assign the value of the first 20 digits to the count variable. Because the fgets () function reads strings, you must convert them to Integers by adding + 0,
This is different from ASP. Strings in ASP can be directly computed with integers without conversion. */
Fclose ($ fp );
// Close the file
}
$ Count ++;
// Increase the count, which is very similar to C.
$ Fp = fopen ($ counter_file, "w ");
// W is the abbreviation of write, which indicates the meaning of writing. open the file as a write.
Fputs ($ fp, $ count );
// Output the Count value to the file
Fclose ($ fp );
Return ($ count );
// Return the Count value
}
?>

<? Php
$ Hit = get_hit ("counter.txt ");
// Call the just-defined counter to process the counter.txt document and assign the result to the hit variable.
Echo "you are the <B>". "$ hit". "</B> guest! ";
// Output the result. The difference between PHP and ASP is that the connection character of ASP is "&", while that of Php is ".".
?>

Also insert this file in the PHP document to be called:

<? Php include ("counter. php");?>

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.