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调用MsSQL Server 2012存储过程获取多结果集(包含output参数)的详解

【PHP Source Code】:复制代码 代码如下:$dbh = new PDO('sqlsrv:server=连接地址;Database=数据库名', 用户名, 密码);try {...

thinkphp中多表查询中防止数据重复的sql语句(必看)

下面先来看看例子: table id name 1 a 2 b 3 c 4 c 5 b 库结构大概这样,这只是一个简单的例子,实际情况会复杂得多。 select *, c...

php获取指定范围内最接近数的方法

本文实例讲述了php获取指定范围内最接近数的方法。分享给大家供大家参考。具体实现方法如下: // Returns the next higher or lower number fu...

PHP伪造referer实例代码

这里就直接给出完整的程序吧,具体的应用可以自己修改。 我这里给出的例子是很简单的,其实可以从这个例子中发展出很多的应用。比如隐藏真实的URL地址……嘿嘿,具体的就自己分析去吧 这里新建一...

php在项目中寻找代码的坏味道(综艺命名)

php在项目中寻找代码的坏味道(综艺命名)

漫画篇 查字典   这个是我比较习惯的方式,打开字典,把中文放进去,看它能给我什么好的名字。             &nb...