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手册中对它的语法定义如下:   for (expr1; expr2; expr3)   statement   下面说说for语句几种有用的变型。   1、无限循环   首先是人尽...

php7基于递归实现删除空文件夹的方法示例

本文实例讲述了php7基于递归实现删除空文件夹的方法。分享给大家供大家参考,具体如下: php版本7.0.4 代码如下: <?php $path = 'd:/'; rm...

不重新编译PHP为php增加openssl模块的方法

复制代码 代码如下:yum -y install openssl /usr/local/bin/是php的安装目录 切换到php安装目录的etx/openssl目录 cd /root/s...

PHP序列化/对象注入漏洞分析

本文是关于PHP序列化/对象注入漏洞分析的短篇,里面讲述了如何获取主机的远程shell。 如果你想自行测试这个漏洞,你可以通过 XVWA 和 Kevgir 进行操作。 漏洞利用的第一步,...

PHP产生随机字符串函数

<?php  /**   * 产生随机字符串   *   * 产生一个指定长度的随机字符串...