本文目录一览:
- 1、使用 PHP 读取文本(TXT)文件 并分页显示
- 2、php读txt写txt
- 3、用php读取txt内容
- 4、php怎么读取txt文本内容存入mysql数据库
- 5、如何用PHP读取TXT文件并且修改
使用 PHP 读取文本(TXT)文件 并分页显示
view source print? ?php // you should save this file as m php session_start(); if ( empty ( $page )) { $page = ;} if (isset( $_GET [ page ])==TRUE) { $page = $_GET [ page ]; } ? head meta equiv= "Content Type" content= "text/; charset=UTF " / title qqview Read Result/title style type= "text/css" ! STYLE {font size: px} STYLE {font size: px} /style /head body table width= " %" bgcolor= "#CCCCCC" tr td ?php if ( $page ){ $counter = file_get_contents ( "example txt" ); // read the file into a string $length = strlen ( $counter ); $page_count = ceil ( $length / ); function msubstr( $str $start $len ){ $strlength = $start + $len ; $tmpstr = "" ; for ( $i = ; $i $strlength ; $i ++) { if (ord( substr ( $str $i ))== x a) { $tmpstr = br / ; } if (ord( substr ( $str $i )) xa ) { $tmpstr = substr ( $str $i ); $i ++; } else { $tmpstr = substr ( $str $i ); } } return $tmpstr ; } // 截取中文字符串 $c =msubstr( $counter ( $page )* ); $c =msubstr( $counter $page * ); echo substr ( $c strlen ( $c ) strlen ( $c ) strlen ( $c )); }? /td /tr /table table width= " %" bgcolor= "#cccccc" tr td width= " %" align= "center" valign= "middle" span class = "STYLE " ?php echo $page ;? / ?php echo $page_count ;? 页 /span/td td width= " %" height= " " align= "left" valign= "middle" span class = "STYLE " ?php echo "a href=m php?page= 首页/a " ; if ( $page != ){ echo "a href=m php?page=" ( $page ) "上一页/a " ; } if ( $page $page_count ){ echo "a href=m php?page=" ( $page + ) "下一页/a " ; } echo "a href=m php?page=" $page_count "尾页/a" ; ? /span /td /tr /table /body / lishixinzhi/Article/program/PHP/201311/21215
php读txt写txt
读可以用:
?php
$abc=file_get_contents("1.txt");
?
textarea name="text"?=str_replace("br /","",$abc);?/textarea
写可以:
?php
$text=nl2br(htmlspecialchars($_POST['text']));
$fp=fopen("1.txt","r);
if(fwrite($fp,$text)) echo 'OK';
else echo 'Failed';
fclose($fp);
?
-----------
乱码的话应该是你写入的编码和读取时的编码不对吧,你将你的程序文件的编码和读取TXT文件的编码设为一样的。
用php读取txt内容
首先fopen读取TXT文件,获取一个文件指针,然后fgets获取一行,再fgets继续读取下一行
官方例子:
?php
$f = fopen ("fgetstest.php", "r");
$ln= 0;
while (! feof ($f)) {
$line= fgets ($f);
++$ln;
printf ("%2d: ", $ln);
if ($line===FALSE) print ("FALSE\n");
else print ($line);
}
fclose ($f);
这个前提是你的$f这个文件指针不能关闭,如果你想在不同请求的情况下实现,那就要吧$f做全局存储了,看看存session可否(我没做过,不确定,你试试看)
php怎么读取txt文本内容存入mysql数据库
第一步,读取txt的文件。假设为a.txt
$content = file_get_content('a.txt'); //读取文件内容存入变量。
第二步,存入数据库
mysql_query("insert 表名 (字段名) values('".$content."'));
Ps:文件是上传的,上传后的临时文件名是:$_FILE['tmp_name']
如何用PHP读取TXT文件并且修改
/**
* 读文件
**/
function read_file($filename)
{
$fp = fopen($filename, "r") or die("couldn't open $filename");
$read = fread($fp, filesize($filename));
fclose($fp);
return $read;
}
/**
* 写文件
**/
function write_file($filename, $buffer)
{
$fp = fopen($filename, "w") or die("couldn't open $filename");
flock( $fp, LOCK_EX );
$write = fputs($fp, $buffer);
flock( $fp, LOCK_UN );
fclose($fp);
return true;
}
/**
* 修改(只是追加内容)
**/
function append_to_file($filename, $buffer)
{
$fp = fopen($filename, "a") or die("couldn't open $filename");
flock( $fp, LOCK_EX );
fputs($fp, $buffer);
flock( $fp, LOCK_UN );
fclose($fp);
return true;
}
/**
* 测试
**/
$str = read_file('test.txt');
echo $str;
write_file('test2.txt', $str);
append_to_file('test2.txt', "ABCD");