PHP关于文件与文件夹(1) 写入文件 文件权限 三、锁定文件
2017-07-11 16:52
531 查看
一、文件权限
总之中的一个切都是为了保证文件夹的安全,保证文件夹的安全比保证文件的安全更重要。
二、写入文件
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.
file_put_contents($file,$_POST['quote'] . PHP_EOL, FILE_APPEND); // Write thedata.
//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>
总之中的一个切都是为了保证文件夹的安全,保证文件夹的安全比保证文件的安全更重要。
二、写入文件
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.
file_put_contents($file,$_POST['quote'] . PHP_EOL, FILE_APPEND); // Write thedata.
//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>
相关文章推荐
- PHP关于文件与目录(1) 写入文件 文件权限 三、锁定文件
- PHP关于文件与目录(1) 写入文件 文件权限 三、锁定文件
- PHP文件锁定写入实例分享
- Android SD卡创建文件夹/文件 SD卡写入内容 SD卡删除文件 计算SD卡文件大小,Android6.0+权限
- PHP文件锁定写入实例教程
- php关于一个文件夹所有的文件复制到另一个文件夹里面去,相同的文件名覆盖
- PHP文件锁定写入实例教程详解
- 关于 IIS7.0下文件写入无权限的解决办法
- apache环境下禁止某文件夹内运行PHP脚本、禁止访问文件或目录执行权限的设置方法
- PHP调用Linux的命令行执行文件压缩命令&&创建文件夹修改权限
- apache环境下禁止某文件夹内运行PHP脚本、禁止访问文件或目录执行权限的设置方法
- php创建文件和文件夹并写入内容
- apache环境下禁止某文件夹内运行PHP脚本、禁止访问文件或目录执行权限的设置方法
- 一个关于“权限正常,但是就是在该文件夹下创建文件失败”
- 取得system32文件夹下面文件的写入权限
- PHP文件锁定写入实例解析
- PHP文件锁定写入实例解析
- 用PHP对文件夹进行访问权限设置(文件防止被下载设置)
- PHP开发中关于文件操作的疑难问答
- 用_access判断文件或文件夹是否存在及其读写权限