PHP About files and directories (1) Write file permissions three, lock files

Source: Internet
Author: User
Tags array count parent directory php and rand file permissions

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

?>



Related Article

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: info-contact@alibabacloud.com 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.