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

Source: Internet
Author: User
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 create, 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":

"Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >



ADD aquotation


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 not being stored due to a systemerror.

';
}

} else {//Failed to enter Aquotation.
print ' Please enter aquotation!

';
}

}//End of submitted IF.

Leave PHP and display the form:
?>

Third, lock the file

File_put_contents ($file, $data, file_append| LOCK_EX); The order in which two variables are used 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.

"Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >



View aquotation


Randomquetation


$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 '

'. Trim ($data [$rand]). '

';File allows content information to be placed in an array, with each element containing a row

?>

  • 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.