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实现的json类实例

本文实例讲述了PHP实现的json类。分享给大家供大家参考。具体如下: 这里注意json_encode只有(PHP 5 >= 5.2.0, PECL json >= 1.2....

PHP表单验证的3个函数ISSET()、empty()、is_numeric()的使用方法

ISSET();——适合于检测是否存在这个参数。 定义和作用范围:用于测试一个变量是否具有值(包括0,FALSE,或者一个空字串,但不能是NULL),即:“http://localhos...

php中配置文件操作 如config.php文件的读取修改等操作

复制代码 代码如下: <?php $name="admin";//kkkk $bb='234'; $db=4561321; $kkk="admin"; ?> 函数定义: 配...

php防止SQL注入详解及防范

一个是没有对输入的数据进行过滤(过滤输入),还有一个是没有对发送到数据库的数据进行转义(转义输出)。这两个重要的步骤缺一不可,需要同时加以特别关注以减少程序错误。对于攻击者来说,进行SQ...

Thinkphp结合AJAX长轮询实现PC与APP推送详解

前言 本文主要给大家介绍的关于Thinkphp结合AJAX长轮询实现PC与APP推送的相关内容,分享出来供大家参考学习,话不多说,来一起看看详细的介绍。 实现逻辑 某个操作(比如新建一条...