Reading Writing

fopen($s1,$s2[,$b]) opens a file $s1 for reading or writing, using the mode $s2. It returns the file handler resource. If $b is set to true, files in the include_path will be searched too. To read a remote URL (eg. http:// or ftp://), allow_url_fopen in php.ini needs to be enabled. $s2 can be:
flag r w pointer file already exists
r Y   Start  
r+ Y Y Start  
w   Y Start Truncate to 0 length
w+ Y Y Start Truncate to 0 length
a   Y End  
a+ Y Y End  
x   Y Start Error
x+ Y Y Start Error
c   Y Start  
c+ Y Y Start  
additional flag
b binary files.
t plain-text files. translates \n to \r\n.

fclose($r) closes an open file pointer opened by fopen(). It returns true on success.

fwrite($r,$s[,$i]) or fputs(……) writes the string $s to the file $r. If $i is specified, writing will stop after that many bytes have been written. This function returns the number of bytes written, or false on error.

file_put_contents($s,$m[,$i]) is identical to calling fopen(), fwrite() and fclose() successively to write data to a file $s. $m can be either a string, an array(implode()), or a stream resource(remaining buffer). This function returns the number of bytes written, or false on error. $i can be any combination of:
FILE_USE_INCLUDE_PATH Search for the file in the include directory.
FILE_APPEND Append data to the file instead of overwriting it.
LOCK_EX Use an exclusive lock.
fputcsv($r,$arr[,$s1=’,’[,$s2=’”’]]) formats the array $arr as CSV and writes it to the file $r. $s1 is the delimiter character, and $s2 is the enclosure character.

fflush($r) forces all buffered output to be written to the file $r. It returns true on success or false on failure.

ftruncate($r,$i) truncates the file $r to length $i. It returns true on success or false on failure.

<!DOCTYPE html><html><head></head>
<body><?php

file_put_contents("test.txt","ABCDEF");

$r=fopen("test.txt","ab");
fwrite($r,"1234567890",3);
fwrite($r,"---");
fflush($r);
ftruncate($r,12);
fclose($r);

$r=fopen("test.txt","c");
fwrite($r,"XXX");
fclose($r);

?></body></html>
fread($r,$i)  reads $i bytes from the file $r and returns the string. fscanf($r,$s[,&$m…]) reads one line from the file $r and stores the values into &$m……, according to the format $s (see sscanf() and sprint()). If &$m is omitted, the values parsed will be returned as an array. Otherwise, the function will return the number of assigned values.

fgetc($r) reads and returns one character from the file $r. fgets ($r[,$i]) reads and returns one line from the file $r. If $i is specified, reading ends when $i-1 bytes, a newline or EOF has been read (whichever comes first). fgetss($r[,$i[,$s]]) is identical to fgets() except that it strips any NUL bytes, HTML and PHP tags from the text it reads. $s specifies the allowed tags. fgetcsv ($r[,$i[,$s1=’,’ [,$s2=’”’[,$s3=’\\’]]]]) reads a line from the file $r, and returns an array containing the fields read. $i must be greater than the longest line in characters. $s1 sets the delimiter character. $s2 sets the enclosure character. $s3 sets the escape character.
file_get_contents($s[,$b=false[,$r[,$i1= -1 [,$i2]]]]) reads the entire file $s into a string. If $b is true, include_path will be searched. $r denotes the context resource and can be skipped by setting it to NULL. $i1 tells where the reading starts. $i2 tells the number of bytes to read. file($s[,$i=0]) splits an entire file $s into lines and stores them into an array. $i can be any combination of:
FILE_USE_INCLUDE_PATH:
Search the include_path.
FILE_IGNORE_NEW_LINES:
Do not add a newline at the end of each array element.
FILE_SKIP_EMPTY_LINES
Skip empty lines.

readfile($s[,$b]) reads a file $s and outputs it. Set $b to true to search the include_path. This function returns the number of bytes read from the file, or false on error. fpassthru($r) reads to EOF on the file $r and outputs the results. It returns the number of bytes read from the file, or false on error.

<!DOCTYPE html><html><head></head>
<body><?php

file_put_contents("test.txt",
                  "1,2,3,4,5
                   a,b,c,d,e
                   @,#,$,%,&");

readfile("test.txt");
echo "<br /><br />";

$r=fopen("test.txt","rb");
echo fread($r,3)."<br />";
echo fgets($r)."<br />";
print_r(fgetcsv($r));
echo "<br />";
fpassthru($r);
echo "<br /><br />";
fclose($r);

echo file_get_contents ("test.txt",false,NULL,0,10)."<br />";
print_r(file("test.txt"));

?></body></html>

1,2,3,4,5 a,b,c,d,e @,#,$,%,&

1,2
,3,4,5 
Array ( [0] => a [1] => b [2] => c [3] => d [4] => e ) 
@,#,$,%,&

1,2,3,4,5 
Array ( [0] => 1,2,3,4,5 [1] => a,b,c,d,e [2] => @,#,$,%,& )
feof($r) returns true if the position indicator in the file $r has reached the end-of-file. ftell($r) returns the current position of the position indicator in the file $r. rewind($r) sets the position indicator in the file $r to the beginning. fseek($r,$i1[,$i2]) sets the position indicator in the file $r to be $i1 bytes away. It returns 0 upon success or -1 otherwise. $i2 can be:

SEEK_SET $i1 bytes from the start
SEEK_CUR $i1 bytes from the current position
SEEK_END $i1 bytes from EOF


flock($r,$i) performs a portable advisory locking on the file $r. By default, this function will block until the requested lock is acquired. It returns true on success or false on failure. $i can be:

LOCK_SH Acquire a shared lock (reader).
LOCK_EX Acquire an exclusive lock (writer).
LOCK_UN Release a lock.

RESETRUNFULL
<!DOCTYPE html><html><head></head>
<body><?php

file_put_contents("test.txt","1234567890");

$r=fopen("test.txt","rb");
fscanf($r,"123%d",$i);
echo $i."<br />";                   // 4567890
echo ftell($r)."<br />";           // 10
var_dump(feof($r));echo "<br />";  // bool(true)
rewind($r);
echo ftell($r)."<br />";           // 0
fseek($r,-3,SEEK_END);
echo ftell($r)."<br />";           // 7
fclose($r);

$r=fopen("test2.txt","wb");
if (flock($r,LOCK_EX)){
   $r2=fopen("test2.txt","wb");
   fwrite($r2,"ABC123");           // nothing written!
   fwrite($r,"DEF");
   fflush($r);
   flock($r,LOCK_UN);
} else {
   echo "Coundn't acquire the lock!";
}
fclose($r); fclose($r2);

?></body></html>