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 中一些最为重要的错误检测方法。 我们将为您讲解不同的错误处理方法: 简单的 "die()" 语句 自定义错误和错误触发器 错误报告 基本的错误处理:使用 die()...

php PDO判断连接是否可用的实现方法

mysql_ping() 检查到服务器的连接是否正常。如果到服务器的连接可用,则返回true,否则返回false。 但PDO不支持mysql_ping()方法,因此需要自己编写代码模拟m...

PHP简单检测网址是否能够正常打开的方法

PHP简单检测网址是否能够正常打开的方法

本文实例讲述了PHP简单检测网址是否能够正常打开的方法。分享给大家供大家参考,具体如下: 这是一个检测网址是否能正常打开的PHP代码,通过下面的代码检测一个网址是否能正常访问,如果正常则...

教你在PHPStorm中配置Xdebug

教你在PHPStorm中配置Xdebug

本教程适用于Laravel项目的使用者,并默认使用Homestead作为开发环境的虚拟机。 1. 确认你已经安装了Xdebug 通过ssh登录你的homestead,执行 ls /etc...

关于session在PHP5的配置文件中的详细设置参数说明

;处理session存取的模式(预设:files) session.save_handler = files ;session档案存放路径(预设:/tmp) session.save_p...