PHP代码
<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
private $aggregatedFiledPath = 'Public/allConfig.txt';
private $updatedFilePath = 'Public/modified/allConfig.txt';
public function index(){
$this->display();
}
public function fileAggregate(){
if (IS_POST) {
$path = I('post.filePath');
$path = str_replace("\\", '/', $path);
$open=fopen($this->aggregatedFiledPath,"w" );
fwrite($open,"\xEF\xBB\xBF");//创建一个utf-8格式的文件
fclose($open);
echo "正在读取文件夹{$path}中的txt文件...<br>";
$this->file_list($path);
}
}
//递归一个目录
private function file_list($path){
if ($handle = opendir($path)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
if (is_dir($path."/".$file)) {
// echo $path."/".$file."<br>";//去掉此行显示的是所有的非目录文件
$this->file_list($path."/".$file);
} else {
// echo $path."/".$file."<br>";
$filePath = $path."/".$file;
$this->readFile($filePath);
}
}
}
}
}
//读取一个文件中的内容
private function readFile($filePath){
$temArr = explode('.', $filePath);
$extentionName = $temArr[count($temArr)-1];
if ($extentionName == 'txt') {
$fh = fopen($filePath, "r");
if (!$fh){
echo "<font color='red'>读取文件{$filePath}失败</font><br>";
}
$contents = fread($fh,filesize($filePath));
fclose($filePath);
echo "读取文件{$filePath}成功<br>";
// echo "<div id='{$filePath}'>".$contents."</div>";
// 设置为utf-8格式在字符串前面加这个"\xEF\xBB\xBF"
$contents = "--FILEPATH(PLEASE DON'T MODIFY THIS ROW!)--".$filePath."--FILEPATH(PLEASE DON'T MODIFY THIS ROW!)--"."\r\n".$contents;
$contents .= "\r\n"; //每个文件结尾加换行
$this->writeTxt($contents);
}
}
//写文件
private function writeTxt($contents){
$open=fopen($this->aggregatedFiledPath,"a" );
fwrite($open,$contents);
fclose($open);
}
//文件更新后导入
public function fileUpdate(){
$filePath = $this->updatedFilePath;
$fh = fopen($filePath, "r");
if (!$fh){
echo "<font color='red' size='20px;'>读取文件{$filePath}失败</font><br>";
return false;
}
$contents = fread($fh,filesize($filePath));
fclose($filePath);
echo "正在读取文件{$filePath}...<br>";
$fileArr = explode("--FILEPATH(PLEASE DON'T MODIFY THIS ROW!)--", $contents);
// $fileArr[0]不知道是什么乱七八糟的东西,可能是头文件之类的,从1开始是路径文本
$fileArrLen = count($fileArr);
for($i=1;$i<$fileArrLen;$i+=2) {
$open=fopen($fileArr[$i],"w" );
$tempStr =explode("\r\n", $fileArr[$i+1],2);//去掉开始的换行符
$content = $tempStr[0].$tempStr[1];
$content = rtrim($content);//去掉最后的换行符
fwrite($open,$content);
fclose($open);
echo "更新文件{$fileArr[$i]}成功<br>";
}
}
}
模板代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>本地文件处理系统</title>
</head>
<script type="text/javascript" src="__PUBLIC__\js\jquery.min.js"></script>
<script type="text/javascript" charset="UTF-8">
<!-- {D:\vstar-MobileClient\Assets\Resources\Data} -->
</script>
<style>
.mybutton:hover{
cursor: pointer;
background-color: ;
}
#showTxtContent{
font-size: 12px;
}
</style>
<body>
<div style="width: 600px;height: 600px;position: absolute;top: 50%;left: 50%;margin-top: -300px;margin-left: -300px;">
<span style="font-size: 20px;">请输入要合成文件的路径:<br></span"><br><input value="D:\fileSys\test" id="filePath" style="height: 30px;width: 400px;font-size: 20px;" name='filePath'><br><br>
<span id="tips_div" style="display: none;"><input value="" id="tips" style="height: 30px;width: 400px;font-size: 20px;" name=''><br><br><span style="color: red;font-size: 18px;">重要操作!请在上面输入框中输入YES以完成后续操作!</span><br><br></span>
<input type="button" value="文件合成" class="mybutton" style="height: 50px;width: 200px;font-size:20px;" onclick="
$('#showTxtContent').html('');
var filePath = $('#filePath').val();
$.post('<{:U("Index/fileAggregate")}>', {filePath: filePath}, function(data) {
$('#showTxtContent').html(data);
});
">
<input type="button" value="文件导入" class="mybutton" style="height: 50px;width: 200px;font-size:20px;" onclick="
$('#showTxtContent').html('');
$('#tips_div').css('display', 'block');
if($('#tips').val() != 'YES'){
return false;
}
$.post('<{:U("Index/fileUpdate")}>', {}, function(data) {
$('#tips_div').css('display', 'none');
$('#tips').val('');
$('#showTxtContent').html(data);
});
">
<div style="font-size:16px;">
<br>
README:本系统用于处理本地文件。<br>输入一个本地路径后,选择<b>文件合成</b>,会递归这个文件夹中的所有TXT格式文件,然后把所有的TXT格式文件中的内容读取合并到一个新的文件中(合并后的文件在\fileSys\Public\allConfig.txt)注意:点击合并文件,后面生成的allConfig.txt会覆盖前面的,建议合并后,把这个文件复制到其它地方进行修改。<br>修改完后,将这个文件复制到\fileSys\Public\modified\这个文件夹下(文件名仍为allConfig.txt),点击<b>文件导入</b>,系统会在这个目录下找到这个文件,然后分别还原修改后的文件。
</div>
</div>
<div id="showTxtContent"></div>
</body>
</html>
用ThinkPHP写的,分别是控制器和视图中的内容。