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中iconv函数转码时截断字符问题的解决方法

iconv是转换编码的,但是在中文转码时候出现显示不全问题。  复制代码 代码如下:  iconv("UTF-8","GB2312//IGNORE",$data)...

PHP 验证身份证是否合法的函数

话不多说,请看代码: function is_idcard( $id ) { $id = strtoupper($id); $regx = "/(^\d{15}$)|(^\d...

PHP数据库操作四:mongodb用法分析

本文实例讲述了PHP数据库mongodb用法。分享给大家供大家参考,具体如下: 传统数据库中,我们要操作数据库数据都要书写大量的sql语句,而且在进行无规则数据的存储时,传统关系型数据库...

php通过sort()函数给数组排序的方法

本文实例讲述了php通过sort()函数给数组排序的方法。分享给大家供大家参考。具体分析如下: sort()函数用于给数组排序,本函数为数组中的单元赋予新的键名。原有的键名将被删除。...

PHP设计模式之装饰器模式定义与用法详解

本文实例讲述了PHP设计模式之装饰器模式定义与用法。分享给大家供大家参考,具体如下: 什么是装饰器模式 作为一种结构型模式, 装饰器(Decorator)模式就是对一个已有结构增加"装饰...