当前位置: 移动技术网 > IT编程>开发语言>PHP > php实现编辑和保存文件的方法

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

2018年05月10日  | 移动技术网IT编程  | 我要评论

本文实例讲述了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程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网