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 function GetVideos($url) {     $ch = curl_init();...

PHP XML操作的各种方法解析(比较详细)

PHP XML操作的各种方法解析(比较详细)

XML是一种流行的半结构化文件格式,以一种类似数据库的格式存储数据。在实际应用中,一些简单的、安全性较低的数据往往使用 XML文件的格式进行存储。这样做的好处一方面可以通过减少与数据库的...

PHP简单实现合并2个数字键数组值的方法

本文实例讲述了PHP简单实现合并2个数字键数组值的方法。分享给大家供大家参考,具体如下: 先要了解一个基础知识点:PHP合并数组+与array_merge的区别分析 <?...

PHP 优化配置——加速你的VBB,phpwind,Discuz,IPB,MolyX第1/2页

让论坛速度更快 PHP加速设置 PHP加速:Zend Optimizer优化PHP程序 Zend Optimizer V2.5.7&nb...

二招解决php乱码问题

二招解决php乱码问题

php网页出现乱码一般是在建立数据库时用的编码和php网页的编码不同造成的, 用phpmyadmin建立的数据库如果你不指定编码他默认是latin1_swedish_ci 编码,既瑞典语...