First, file permissions
In short, everything is to ensure the security of the directory, the security of the directory is more important than to ensure the security of the file.
Second, write the document
File_put_contents ($file, $data); //If no words will be created, and some words overwrite the original document;
File_put_contents ($file, $data, file_append); //No words will be created, some words appended to the back;
File_put_contents ($file, $data. Php_eol,file_append);/There is a line change
"Example":
<title></title>
Identify the file to use:
$file = '. /quotes.txt '; //This file is best placed in the parent directory, secure.
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 is stored due to a systemerror.
';
}
else {//Failed to enter Aquotation.
print ' Please Enter aquotation!
';
}
}//end of submitted IF.
Leave PHP and display the form:
?>
Enter your quotationhere.
Third, lock the file
File_put_contents ($file, $data, FILE_APPENDLOCK_EX); //The use order of two variables is irrelevant
Lock_sh shared locks for reading
LOCK_EX exclusive lock for write
Lock_un release a lock
LOCK_NB non-blocking lock
Iv. reading files
The first method: $data =file_get_contents ($file); //Read by a string
The second method: $data =file ($file); //read each row of data, more commonly used
"Example": //Add: A cautious point can use the is_readable () function to test whether the file can be read in front of the filename 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 random number
$rand = rand (0, ($n-1));
Print the quotation:
print '
'. Trim ($data [$rand]). '
'; File allows the content information to be placed in an array, with each element containing a row
?>