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程序设计有所帮助。

相关文章

EPSON打印机 连供墨水系统 维修有哪些保养窍门第1/2页

这种堵塞会导致喷出的墨滴变小或喷出的墨水有明显的分叉现象,使墨滴在纸上的覆盖率不足,同时还会导致喷出的墨水飞斜,在纸上的定位产生偏移,这样打印出的图稿就让人感觉颗粒偏粗。严重的堵塞就造成...

php判断并删除空目录及空子目录的方法

本文实例讲述了php判断并删除空目录及空子目录的方法。分享给大家供大家参考。具体实现方法如下: 步骤如下: 1.遍历目录及子目录 2.使用 scandir 判断目录是否为空,为空则使用r...

php简单统计在线人数的方法

本文实例讲述了php简单统计在线人数的方法。分享给大家供大家参考,具体如下: <?php //首先你要有读写文件的权限 //本程序可以直接运行,第一次报错,以缶涂梢&#...

php中长文章分页显示实现代码

欢迎交流!实现代码如下: 复制代码 代码如下: <?php include('include/config.php'); ?> <?php /** *Author:乌鸟...

PHP strtotime函数详解

先看手册介绍: strtotime — 将任何英文文本的日期时间描述解析为 Unix 时间戳 格式:int strtotime ( string $time [, int $now ]...