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":
<! 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 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 ' <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-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.
<! 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 can place content information in an array, with each element containing a row
?>
</body>
PHP About files and directories (1) Write file permissions three, lock file