One, file permissions
in short, a cut is to ensure the security of the folder, to ensure that the security of the folder than to ensure the security of the file is more important.
Second, write the file
file_put_contents ($file, $data); //Assume that no words will be created. Some words overwrite the original document;
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
"Sample":
<! DOCTYPE HTML PUBLIC "-//w3c//dtd XHTML1.0 transitional//en"
" http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
http://www.w3.org/1999/xhtml"xml:lang= "en" lang= "en" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title>add aquotation</title>
<body>
<?php//Script 11.1-add_quote.php
Identify the file to use:
$file = '. /quotes.txt '; //This file is best placed in the parent folder, 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 ' <p>your quotation has beenstored.</p> ';
} else {//Could Notopen the file.
print ' <p style= ' color:red ; " >yourquotation could not being stored due to a systemerror.</p> ';
}
} else {//Failed to enter Aquotation.
print ' <pstyle= ' color:red ; " >please enter aquotation!</p> ';
}
}//End of submitted IF.
Leave PHP and display the form:
?>
<form action= "add_quote.php" method= "POST" >
<textarea name= "quote" rows= "5" cols= "+" >enter your quotationhere.</textarea><br/>
<input type= "Submit" name= "Submit" value= "Add this quote!"/>
</form>
</body>
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-clogging lock
Four, read the file
The first method: $data =file_get_contents ($file); //follow a string to read
Another method: $data =file ($file); //read each row of data, more often use
"Example": //supplement: Focus on the ability to use the is_readable () function before the file function to test whether the document can be read
<! DOCTYPE HTML PUBLIC "-//w3c//dtd XHTML1.0 transitional//en"
" http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd ",
http://www.w3.org/1999/xhtml "xml:lang=" en "lang=" en ",
<meta http-equiv=" Content-type "content=" text/html; Charset=utf-8 "/>
<title>view aquotation</title>
<body>
<?php
$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 ' <p> ' trim ($data [$rand]). ' </p> '; //file enables content information to be placed in an array, with each element containing a single line
?>
</body>
PHP about files and folders (1) Write file permissions three, lock file