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程序设计有所帮助。

相关文章

探讨file_get_contents与curl效率及稳定性的分析

做过好多抓取别家网站内容的产品,习惯了使用方便快捷的file_get_contents函数,但是总是会遇到获取失败的问题,尽管按照手册中的例子设置了超时,可多数时候不会奏效:复制代码 代...

php网站来路获取代码(针对搜索引擎)

复制代码 代码如下:function get_referer(){ $se = 0; $url = $_SERVER["HTTP_REFERER"]; //获取完整的来路URL $str...

微信公众号点击菜单即可打开并登录微站的实现方法

微信公众号点击菜单即可打开并登录微站的实现方法

本文实例讲述了微信公众号点击菜单即可打开并登录微站的实现方法。分享给大家供大家参考。具体分析如下: 总体来说,微信公众号点击菜单即可打开并登录微站实现步骤比较复杂,但很多微站在己用上了,...

PHPExcel导出2003和2007的excel文档功能示例

本文实例讲述了PHPExcel导出2003和2007的excel文档功能。分享给大家供大家参考,具体如下: require_once 'common/excel/PHPExcel.p...

Mac系统下安装PHP Xdebug

Mac系统下安装PHP Xdebug

Mac下安装PHP调试工具Xdebug 安装步骤 brew install php70 brew install php70-xdebug php -i | grep...