php实现编辑和保存文件的方法

yipeiwu_com5年前PHP代码库

本文实例讲述了php实现编辑和保存文件的方法。分享给大家供大家参考。具体如下:

save_file.php:

<?php 
session_start(); 
$handle = fopen($_POST['original_file_name'], "w"); 
$text = $_POST['file_contents']; 
if(fwrite($handle, $text) == FALSE){ 
  $_SESSION['error'] = '<span class="redtxt">There was an error</span>'; 
}else{ 
  $_SESSION['error'] = '<span class="redtxt">File edited successfully</span>'; 
} 
fclose($handle); 
header("Location: ".$_POST['page']); 
?>

read_file.php:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<form action="savecontents.php" method="post">
<textarea name="file_contents" style="width:700px;height:600px;">
<?php 
$fileName = "location/of/orignal/file/my_file.php"; 
$handle = fopen($fileName, "r"); 
while (!feof($handle)){ 
  $text = fgets($handle); 
  echo $text; 
} 
?> 
</textarea>
<input type="hidden" value=" <? echo $fileName; ?> " name="original_file_name" />
</form>
<body>
</body>
</html>

希望本文所述对大家的php程序设计有所帮助。

相关文章

PHP使用栈解决约瑟夫环问题算法示例

本文实例讲述了PHP使用栈解决约瑟夫环问题算法。分享给大家供大家参考,具体如下: 约瑟夫环问题: 39 个犹太人与Josephus及他的朋友躲到一个洞中,39个犹太人决定宁愿死也不要被敌...

php 注册时输入信息验证器的实现详解

1、对输入信息进行验证的类(主要用于验证用户名,密码,重复密码,邮箱,可添加其它功能)复制代码 代码如下:<?php/** * Validator for Registe...

Apache下禁止php文件被直接访问的解决方案

Apache下禁止php文件被直接访问的解决方案

  一开始,我想在重写规则里直接禁止php后缀的URL被访问。但后来发现重写规则是递归调用的,如果在重写规则里直接禁止php,那么重写到php文件的规则也会失效。RewriteEngin...

php对gzip文件或者字符串解压实例参考

      其实php对gzip解压很简单,用内置的gzdecode函数就可以了,不过很可惜我配置了半天也无法支持gzdeco...

用PHP实现读取和编写XML DOM代码

复制代码 代码如下: // 用 DOM 读取 XML $doc = new DOMDocument(); $doc->load(‘test.xml'); $books = $doc...