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

yipeiwu_com6年前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  $str = file("http://tq.tom.com/china/index.html");  $...

解析PHP计算页面执行时间的实现代码

如下所示:复制代码 代码如下:<?php  $t = new executeTime;  phpinfo();  class executeTime{...

php实现通用的从数据库表读取数据到数组的函数实例

本文实例讲述了php实现通用的从数据库表读取数据到数组的函数。分享给大家供大家参考。具体分析如下: 此函数不关心表结构,只需要指定表名、结构和查询条件既可以对表进行通用查询操作,非常实用...

PHP+SQL 注入攻击的技术实现以及预防办法

总结一下经验。在我看来,引发 SQL 注入攻击的主要原因,是因为以下两点原因:   1. php 配置文件 php.ini 中的 magic_quotes_gpc 选项没有打开,被置为...

PHP函数之error_reporting(E_ALL ^ E_NOTICE)详细说明

举例说明: 在Windows环境下:原本在php4.3.0中运行正常的程序,在4.3.1中为何多处报错,大体提示为:Notice:Undefined varialbe:变量名称. 例如有...