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

Source: Internet
Author: User

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


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":




<title></title>


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 '

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

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

?>

http://www.bkjia.com/PHPjc/907372.html www.bkjia.com true http://www.bkjia.com/PHPjc/907372.html techarticle php about files and directories (1) Write file permissions three, lock file one, file permissions in short, everything is to ensure the security of the directory, to ensure the security of the directory than the guarantee file ...

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