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二维数组实现去除重复项的方法。分享给大家供大家参考,具体如下: 对于如下二维数组,要求对其进行去重: $arr = array( '0'=>arr...

mongodb和php的用法详解

Mognodb数据库连接. 默认格式 $m = new Mongo(); //这里采用默认连接本机的27017端口,当然也可以连接远程主机如 192.168.0.4:27017,如...

基于php中使用excel的简单介绍

基于php中使用excel的简单介绍

在工作中需要处理多语言的翻译问题,翻译都是写在excel表格里面。为了处理方便我就保留中文和英文两列。 这样需要将这些数据从excel中取出来,然乎保存在excel的数组中,通过使用循环...

discuz Passport 通行证 整合笔记

discuz Passport 通行证 整合笔记

太简单了,但时间长了,记不得,浪费我半小时找资料,深刻体会好记性不如烂笔头!!今天把passport文挡贴上,防止以后忘记!!记住,网上找到自己需要的资料也要耗时间的!!!!!! Pas...

PHP Memcached应用实现代码

PHP Memcached应用实现代码

肖理达 (KrazyNio AT hotmail.com), 2006.04. 06, 转载请注明出处 一、memcached 简介 在很多场合,我们都会听到 memcached 这个名...