PHP About files and directories (1) Write file permissions three, lock file
One, file permissions
In short, everything is to ensure the security of the directory, to ensure that the security of the directory is more important than the security of the file.
Second, write the file
File_put_contents ($file, $data); //If not, will be created, some words overwrite the original file;
File_put_contents ($file, $data, file_append); //No words will be created, some words appended in the back;
File_put_contents ($file, $data. Php_eol,file_append);//Have a newline
"Example":
<title></title>
Identify the file to use:
$file = '. /quotes.txt '; //This file is best placed in the parent directory, security.
Check for a form submission:
if ($_server[' request_method ' = = ' POST ') {//Handle theform.
if (!empty ($_post[' quote ')) && ($_post[' quote ']! = ' Enter yourquotation here. ')) {//Need some thing to write.
if (is_writable ($file)) {//Confirm that the file iswritable.
file_put_contents ($file, $_post[' quote ' ). Php_eol, File_append); Write Thedata.
//print amessage:
print '
Your quotation has beenstored.
';
} else {//Could Notopen the file.
print '
Yourquotation could not being stored due to a systemerror.
';
}
} else {//Failed to enter Aquotation.
print ' Please enter aquotation!
';
}
}//End of submitted IF.
Leave PHP and display the form:
?>
Third, lock the file
File_put_contents ($file, $data, file_append| LOCK_EX); The use order of two variables is irrelevant
Lock_sh shared lock for reading
LOCK_EX exclusive lock for writing
Lock_un release a lock
LOCK_NB non-blocking lock
Four, read the file
The first method: $data =file_get_contents ($file); //follow a string to read
The second method: $data =file ($file); //read each row of data, more commonly
"Example": //supplement: Be careful you can use the is_readable () function to test whether the file can be read in front of the file function.
Http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd>
<title></title>
Randomquetation
$data =file ('.. /quotes.txt ');
Count the number of items in the array:
$n = count ($data);
Pick a random item: produces a randomly-generated number
$rand = rand (0, ($n-1));
Print the quotation:
print '
'. Trim ($data [$rand]). '
'; File allows content information to be placed in an array, with each element containing a row
?>
http://www.bkjia.com/PHPjc/907372.html www.bkjia.com true http://www.bkjia.com/PHPjc/907372.html techarticle php about files and directories (1) Write file permissions three, lock file one, file permissions in short, everything is to ensure the security of the directory, to ensure the security of the directory than the guarantee file ...