PHP file read/write instance code-PHP source code

Source: Internet
Author: User
This tutorial provides a lot of information about how to use php to operate files, if a file is intercepted, read the file content, format the output file content, lock the file function, and open the file in writing mode, this tutorial provides a lot of instructions on how to use php to operate the file, if a file is intercepted, read the file content, format the output file content, lock the file content, and open the file in writing mode.

Script ec (2); script

Csv files.
*/
Using fpw.fopen('test.txt ', 'R'); // open the file
$ Flag = ftruncate ($ fp, 5); // capture a file
If ($ flag) // determines whether the object is intercepted successfully
{
Echo "the file has been intercepted! ";
}
Else
{
Echo "An error occurred while intercepting the file! ";
}
Fclose ($ fp); // close the file

/*

*/

Using fpw.fopen('test.txt ', 'R'); // open the file in read-only mode
$ Data = fgets ($ fp, 4096); // read the file content
Fseek ($ fp, 100); // move the file pointer to the first offset bit.
Fclose ($ fp); // close the file

/*
*/

$ Fp = fopen ("test.txt", "r"); // open the file
$ Contents = fscanf ($ fp, "% st % sn"); // format the output file content
Print_r ($ contents); // output content
Fclose ($ fp); // close the file

/*
*/
Foreach (glob ("*. txt") as $ filename)
{
Echo "file name:". $ filename;
Echo"
";
Echo "file size:". filesize ($ filename). "n ";
Echo"

";
}
// Match all text files


Function lockfile ($ fp) // User-Defined lock file function
{
Flock ($ fp, lock_ex) or die ("cannot flock file"); // locked exclusively, only the current process can access the file
}
Function unlockfile ($ fp) // custom unlock function
{
Flock ($ fp, lock_un) or die ("cannot unlock file"); // release lock
}
$ Fp = fopen ("test.txt", "w +"); // open the file
Lockfile ($ fp); // lock the file
Fwrite ($ fp, "hello, php tutorial"); // write the file
Unlockfile ($ fp); // unlock
Fclose ($ fp); // close the file

//

$ File = "test.txt"; // definition file
$ Fp = fopen ($ file, "w"); // open the file as a write
$ Date = "hello world! "; // Define a string
Fputs ($ fp, $ date); // write the string to the specified file
Fclose ($ fp); // close the file

//

$ List = array (
'A, B, c, D ',
'1, 2, 3'
); // Define an array as the input function parameter
Export fp1_fopen('test.csv ', 'w'); // open a csv file as a write
Foreach ($ list as $ line) // traverses the array and writes the csv file with "," as the delimiter
{
Fputcsv ($ fp, split (',', $ line ));
}
$ Date = fread ($ fp, 4096); // read the file content
Echo $ date; // output file content
Fclose ($ fp); // close the file

//

$ Fp = fopen ("test.txt", "r"); // open the file
Echo fpassthru ($ fp) // read file data to the buffer zone
Fclose ($ fp); // close the file

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.