首页  >  文章  >  后端开发  >  php针对文件的读取、编辑和保存的操作

php针对文件的读取、编辑和保存的操作

墨辰丷
墨辰丷原创
2018-06-08 16:11:162855浏览

本篇文章主要介绍php针对文件的读取、编辑和保存的操作,感兴趣的朋友参考下,希望对大家有所帮助。

本文实例讲述了php实现编辑和保存文件的方法,具体如下:

save_file.php:

<?php 
session_start(); 
$handle = fopen($_POST[&#39;original_file_name&#39;], "w"); 
$text = $_POST[&#39;file_contents&#39;]; 
if(fwrite($handle, $text) == FALSE){ 
  $_SESSION[&#39;error&#39;] = &#39;<span class="redtxt">There was an error</span>&#39;; 
}else{ 
  $_SESSION[&#39;error&#39;] = &#39;<span class="redtxt">File edited successfully</span>&#39;; 
} 
fclose($handle); 
header("Location: ".$_POST[&#39;page&#39;]); 
?>

read_file.php:

<!DOCTYPE html>
<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程序中字符串长度判断

以上是php针对文件的读取、编辑和保存的操作的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn