chmod)fopen($filename, $mode)fopen returns a handle that is used for
reading and writing.fclose($handle)opendir($dirname)closedir($dirname)fopen (no error handling in
this example):
$file = "/dir/file.txt"; $newfile = fopen($file, "w+"); fclose($newfile);
fgets-function reads one line at a time.$file = "file.txt"; $handle = fopen($file, "r"); print fgets($handle); fclose($handle);
$handle = fopen("/tmp/inputfile.txt", "r");
while (!feof($handle)) {
$buffer = fgets($handle);
print $buffer;
}
fclose($handle);
$handle = fopen("file.txt", "a");
fputs($handle, "line\n");
fclose($handle);
flock() functions locks a fileflock() takes to parameters: file handle and operation. Operation is
one of the following:
$fp = fopen("/tmp/lock.txt", "w+");
flock($fp, LOCK_EX)
fputs($fp, "Write something here\n");
flock($fp, LOCK_UN);
fclose($fp);
if(!($fp = fopen("/tmp/lock.txt", "w+"))) {
print "Cannot open file.";
}
if(!(flock($fp, LOCK_EX))) {
print "Cannot lock the file";
}
if(!(fputs($fp, "Write something here\n"))) {
print "Cannot write to a file";
}
if(!(flock($fp, LOCK_UN))) {
print "Cannot unlock the file";
}
if(!(fclose($fp))) {
print "Cannot close the handle";
}
function file_put_contents($file, $data) {
if(!($fp = fopen($file, "w+"))) {
return false;
}
.
.
return true;
}
file_put_contents("file.txt", "Hello!");
http://fi.php.net/file_put_contentsfile_put_contents(file,data,mode,context) writes a string to a file.file_put_contents("file.txt", "Hello!", LOCK_EX);