PHP关于文件与目录(1) 写入文件 文件权限 三、锁定文件
2014-11-11 22:25
393 查看
一、文件权限总之一切都是为了保证目录的安全,保证目录的安全比保证文件的安全更重要。二、写入文件file_put_contents($file,$data); //如果没有的话会创建,有的话覆盖原文件;file_put_contents($file,$data,FILE_APPEND); //没的话会创建,有的话追加在后面;file_put_contents($file,$data.PHP_EOL,FILE_APPEND);//有换行【例子】: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd
"><html xmlns="http://www.w3.org/1999/xhtml
"xml:lang="en" lang="en"><head><meta http-equiv="content-type"content="text/html; charset=utf-8" /><title>Add AQuotation</title></head><body><?php // Script 11.1 -add_quote.php // 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. //Print amessage: print'<p>Your quotation has beenstored.</p>';
} else { // Could notopen the file.print'<p style="color: red;">Yourquotation could not be 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="30">Enter your quotationhere.</textarea><br/><input type="submit" name="submit"value="Add This Quote!" /></form></body></html>
三、锁定文件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()函数测试下是否可读这个文件 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd
"><html xmlns="http://www.w3.org/1999/xhtml
" xml:lang="en"lang="en"><head><meta http-equiv="content-type"content="text/html; charset=utf-8" /><title>View AQuotation</title></head><body><h1>RandomQuetation</h1><?php$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 '<p>' .trim($data[$rand]) .'</p>'; //file可以使内容信息放置在一个数组中,每个元素都包含了一行 ?></body></html>本文来自:爱好Linux技术网
本文链接:http://www.ahlinux.com/php/8687.html
"><html xmlns="http://www.w3.org/1999/xhtml
"xml:lang="en" lang="en"><head><meta http-equiv="content-type"content="text/html; charset=utf-8" /><title>Add AQuotation</title></head><body><?php // Script 11.1 -add_quote.php // 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. //Print amessage: print'<p>Your quotation has beenstored.</p>';
} else { // Could notopen the file.print'<p style="color: red;">Yourquotation could not be 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="30">Enter your quotationhere.</textarea><br/><input type="submit" name="submit"value="Add This Quote!" /></form></body></html>
三、锁定文件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()函数测试下是否可读这个文件 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd
"><html xmlns="http://www.w3.org/1999/xhtml
" xml:lang="en"lang="en"><head><meta http-equiv="content-type"content="text/html; charset=utf-8" /><title>View AQuotation</title></head><body><h1>RandomQuetation</h1><?php$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 '<p>' .trim($data[$rand]) .'</p>'; //file可以使内容信息放置在一个数组中,每个元素都包含了一行 ?></body></html>本文来自:爱好Linux技术网
本文链接:http://www.ahlinux.com/php/8687.html
相关文章推荐
- PHP关于文件与目录(1) 写入文件 文件权限 三、锁定文件
- PHP关于文件与文件夹(1) 写入文件 文件权限 三、锁定文件
- apache限制某个目录下的php文件没有执行权限
- 关于Linux系统中文件或目录的s、t权限位的说明
- apache禁止访问文件或目录执行权限、禁止运行脚本PHP文件的设置方法
- PHP文件锁定写入实例解析
- PHP文件锁定写入实例教程
- PHP文件或目录权限检查函数
- 写文件前, 检查目录写权限(PHP)
- apache禁止访问文件或目录执行权限、禁止运行脚本PHP文件的设置方法
- 关于Nginx+PHP的虚拟主机目录权限控制的探究
- nginx与Apache禁止目录执行php文件权限
- PHP chmod 函数与批量修改文件目录权限
- 您不具备查看该目录或页面的权限,因为访问控制列表(ACL)对Web服务器上的该资进行了配置-关于IIS新追加文件无法访问的原因
- 批量锁定、解锁网站目录写入权限的脚本(linux)
- Linux关于文件和目录的访问权限
- php中判断文件空目录是否有读写权限的函数代码
- PHP文件锁定写入实例分享
- PHP文件锁定写入实例解析
- apache限制某个目录下的php文件没有执行权限