‘壹’ 用php,怎么修改txt文本内的内容
<?php
header("Content-type:text/html;charset=gbk");
if(isset($_POST['submit'])){
$editContent=$_POST['editContent'];//获取输入框的内容
$res=file_put_contents("test.txt",$editContent);//执行修改
if($res){
echo'内容修改成功!';
}else{
echo'内容修改失败!';
}
}else{
echo'请做出修改....';
}
?>
<formmethod="post"action="">
<textareaname="editContent"cols="100"rows="15">
<?phpechofile_get_contents("test.txt")?>
</textarea>
<buttonname="submit">确认</button>
</form>
‘贰’ php 如何创建txt文件
有凯悔两种方法:
file_put_contents("/tmp/a.txt","盯此正");
system("cd /tmp; touch 1.txt"扒渣); 这种只时候在服务器执行,本地不可以
‘叁’ php 如何创建txt文件
看手册,文件操作部分,比如 file_put_contents 就能满足要求
<?php
$file='people.txt';
//Thenewpersontoaddtothefile
$person="JohnSmith ";
//Writethecontentstothefile,
//usingtheFILE_
//andtheLOCK_
file_put_contents($file,$person,FILE_APPEND|LOCK_EX);
?>
‘肆’ Thinkphp下导入千万级txt文件好方法
$fp=fopen($file,"r");
$num=10;
$chunk=4096;
$fs=sprintf("%u",filesize($file));
$max=(intval($fs)==PHP_INT_MAX)?PHP_INT_MAX:filesize($file);
for($len=0;$len<$max;$len+=$chunk){
$seekSize=($max-$len>$chunk)?$chunk:$max-$len;
fseek($fp,($len+$seekSize)*-1,SEEK_END);
$readData=fread($fp,$seekSize).$readData;
if(substr_count($readData,"n")>=$num+1){
preg_match("!(.*?n){".($num)."}$!",$readData,$match);
$data=$match[0];
break;
}
}
fclose($fp);
echo$data;
只要不读到内存操作就好办
‘伍’ 如何用php向txt写入数据
/*先取出*/
$string = file_get_contents("1.txt");
$newstring; // 新数据
if (empty($string)) {
$string = $newstring;
} else {
$string .= '|' . $newstring;
}
file_put_contents("1.txt", $string);