PHP about files and folders (1) Write file permissions three, lock file

Source: Internet
Author: User

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


<! DOCTYPE HTML PUBLIC "-//w3c//dtd XHTML1.0 transitional//en"
"" >"xml:lang= "en" lang= "en" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title>add aquotation</title>
<?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!"/>


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"
  " ", "xml:lang=" en "lang=" en ",
  <meta http-equiv=" Content-type "content=" text/html; Charset=utf-8 "/>
  <title>view aquotation</title>
$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


PHP about files and folders (1) Write file permissions three, lock file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.