php实现编辑和保存文件的方法

yipeiwu_com5年前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中oci8扩展

给大家分享个php操作Oracle的操作类 Oracle_db.class.php <?php class Oracle_db{ public $link; p...

浅析PHP数据导出知识点

最近在做后台管理的项目,后台通常有数据导出到 excel 的需要,经过之前搜索通常推荐使用的是 php excel ,我经常使用的是 laravel ,对于 php excel 也有很好...

php use和include区别总结

PHP中use、include的区别 1、include是导入文件,如果找不到文件,include会报warning,继续执行。 2、use是使用命名空间,相当于java中的导包,前提是...

php采用curl实现伪造IP来源的方法

本文实例讲述了php采用curl实现伪造IP来源的方法。可以实现伪造IP来源, 伪造域名, 伪造用户信息,分享给大家供大家参考。具体实现方法如下: 定义伪造用户浏览器信息HTTP_USE...

10个超级有用值得收藏的PHP代码片段

尽管PHP经常被人诟病,被人贬低,被人当玩笑开,事实证明,PHP是全世界网站开发中使用率最高的编程语言。PHP最大的缺点是太简单,语法不严谨,框架体系很弱,但这也是它最大的优点,一个有点...