PHP file read and write operation instance code

Source: Internet
Author: User
Tags foreach flock php tutorial

CSV file, and so on.
*/
$fp =fopen (' Test.txt ', ' R '); Open File
$flag =ftruncate ($FP, 5); Intercepting files
if ($flag)//Whether the decision file is intercepted successfully
{
echo "File interception successful! ";
}
Else
{
echo "File interception failed! ";
}
Fclose ($FP); Close File

/*

*/

$fp =fopen (' Test.txt ', ' R '); Open a file as read-only
$data =fgets ($fp, 4096); Read File contents
Fseek ($fp, 100); Move the file pointer to the 100th offset shift
Fclose ($FP); Close File

/*
*/

$FP =fopen ("Test.txt", "R"); Open File
$contents =fscanf ($fp, "%st%st%sn"); Format output File contents
Print_r ($contents); Output content
Fclose ($FP); Close File

/*
*/
foreach (Glob ("*.txt") as $filename)
{
echo "FileName:". $filename;
echo "<br>";
echo "File Size:". FileSize ($filename). " n ";
echo "<p>";
}
Match all text files


function Lockfile ($FP)//Custom lock file functions
{
Flock ($FP, lock_ex) or die ("Cannot flock file"); Locked exclusively, only the current process has access to the file
}
function Unlockfile ($FP)//Custom unlock functions
{
Flock ($FP, Lock_un) or Die ("Cannot unlock file"); Release lock
}
$FP =fopen ("Test.txt", "w+"); Open File
Lockfile ($FP); Lock file
Fwrite ($fp, "hello,php tutorial"); Write to a file
Unlockfile ($FP); Unlock
Fclose ($FP); Close File

//

$file = "Test.txt"; Definition file
$FP =fopen ($file, "w"); To open a file as a write
$date = "Hello world!"; Define String
Fputs ($fp, $date); Writes a string to the specified file
Fclose ($FP); Close File

//

$list =array (
' A,b,c,d ',
' 1,2,3 '
); Defines an array as an argument to an incoming function
$fp =fopen (' Test.csv ', ' W '); Open a CSV file as a write
foreach ($list as $line)//traversing an array, writing a CSV file as a "," number as a delimiter
{
Fputcsv ($fp, Split (', ', $line));
}
$date =fread ($fp, 4096); Read File contents
Echo $date; Output file contents
Fclose ($FP); Close File

//

$fp =fopen ("Test.txt", "R");          //Open File
Echo fpassthru ($fp            //reads the file data to the buffer
Fclose ($fp);            //closes 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.