您好,欢迎访问一九零五行业门户网

PHP关于文件与目录(1) 写入文件 文件权限 三、锁定文件_PHP教程

php关于文件与目录(1) 写入文件 文件权限 三、锁定文件 一、文件权限
总之一切都是为了保证目录的安全,保证目录的安全比保证文件的安全更重要。
二、写入文件
file_put_contents($file,$data); //如果没有的话会创建,有的话覆盖原文件;
file_put_contents($file,$data,file_append); //没的话会创建,有的话追加在后面;
file_put_contents($file,$data.php_eol,file_append);//有换行
【例子】:
// identify the file to use:
$file ='../quotes.txt'; //这个文件最好放在父目录中,安全。
// 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 be stored due to a systemerror.
';

} else { // failed to enter aquotation.
print 'please enter aquotation!=color:>
';

} // end of submitted if.
// leave php and display the form:
?>
三、锁定文件
file_put_contents($file,$data,file_append|lock_ex); //两个变量的使用顺序无关紧要
lock_sh 用于读取的共享锁
lock_ex 用于写入的独享锁
lock_un 释放一个锁
lock_nb 非阻塞锁
四、读取文件
第一种方法:$data=file_get_contents($file); //按照一个字符串来读取
第二种方法:$data=file($file); //读取每一行的数据,较为常用
【例】: //补充:谨慎点可以在file函数前使用is_readable()函数测试下是否可读这个文件
http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd>
randomquetation
$data =file('../quotes.txt'); 
// count the number of items in the array:
$n = count($data);
// pick a random item: 产生一个随机数
$rand = rand(0, ($n -1));
// print the quotation:
print '
' .trim($data[$rand]) .'
'; //file可以使内容信息放置在一个数组中,每个元素都包含了一行 
?>
http://www.bkjia.com/phpjc/907372.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/907372.htmltecharticlephp关于文件与目录(1) 写入文件 文件权限 三、锁定文件 一、文件权限 总之一切都是为了保证目录的安全,保证目录的安全比保证文件的...
其它类似信息

推荐信息