导航:首页 > 编程语言 > php打开txt

php打开txt

发布时间:2022-12-27 16:39:33

❶ 用php读取txt内容

$file
=
"t.txt";//要读的文本
$fp
=
@fopen($file,
'r');//以直读(r)方式打开文件【注意,是r不是a,具体参考手册fopen函数】
$content
=
@fread($fp,
filesize($file));//读取全部(filesize($file))内容
fclose($fp);//关闭文件
$content
=
preg_replace('/[\n\r]/is',
'<br/>',
$content);//将换行符换成HTML标签的换行
//你上例中的123456789会换成123<br/>456<br/>789
echo
$content;//输出文件

❷ 使用 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> </> lishixin/Article/program/PHP/201311/21215

❸ php打开一个txt文件,并提取每一行的前十个字符串。然后创建保存到新的txt文件。

第一行前10个,不就是所有的前10吗
<?php
$str=file_get_contents("test.txt");
file_put_contents("test1.txt",mb_strcut($str,0,10));
?>

❹ php分割txt文件

<?php

/**

* 104857600=100mb

* 10485760=10mb

* 10485760/5=2mb

* 读取的定义为源文件

* 写入的定义为目标文件

*/

$size=filesize('201808.txt');

$i=0;

$block_info = [];

while($size>0){

    //组建规格数组

    $block_info[]=[

    'size' => $size>=104857600?104857600:$size,//目标文件大小

    'file' => str_replace('.txt', '','201808.txt').'.'.($i++).'.txt'//目标文件名

    ];

    $size-=104857600;//源文件大小递减

}

$fp = fopen('201808.txt',"rb");//打开源文件

foreach($block_info as $k => $v){

    $handle = fopen($v['file'],'wb');//创建并打开目标文件

    fwrite($handle,fread($fp,$v['size']));//fwrite函数写入目标文件,fread函数读取源文件并读取设定字节数

    fclose($handle);//关闭目标文件

    unset($handle);//销毁变量

}

fclose ($fp);//结束关闭源文件

unset($fp);//销毁变量

echo "ok";//输出结果

❺ 求教php使用TXT数据库(读取和修改文本)

我建议一下吧,文本数据库的例子本来太多,但是为了逻辑简化,最好通过专门接口实现文件与数据的转换,可以采用我下面的模板编写:

<?php
//文件最前面定义两个全局变量,数据库文件名和用户数组
$pwd_db_file='db.txt';
$UserPassword=array();

//下面的pwd_db_read函数,把文件内容读入到全局数组中
function pwd_db_read(){
global $pwd_db_file, $UserPassword;
$fp=fopen($pwd_db_file,'r');
while ($s=fgets($fp)){
list($usr,$pwd)=explode('|', $s);
$UserPassword[$usr]=$pwd;
}
fclose($fp);
}

//下面的pwd_db_write函数保存数组内容到文件中
function pwd_db_write(){
global $pwd_db_file, $UserPassword;
fp=fopen($pwd_db_file, 'w');
foreach ($UserPassword as $usr=>$pwd)
fputs($fp,"$usr|$pwd\n");
fclose($fp);
}

//有了上面的全局变量和函数,要写什么功能都简单
//下面假释本脚本调用的时候通过reg.php?job=add&user=...&pass=...
//的格式进行调用,job为add表示添加用户,del表示删除,modi表示修改
//另外的user和pass表示用户名或者密码,job不是以上内容表示登录

//主程序一开始就打开数据库
pwd_db_read();
//下面判断功能
if ($jon=='add'){
if (array_key_exists($user,$UserPassword)) echo "用户 $user 已经存在!"
else $UserPassword[$user]=$pass;//就一句话,简单吧
}elseif (job=='del'){
unset($UserPassword[$user]);//你自己考虑编写是否确认删除的内容
}elseif ($job=='modi'){
if (array_key_exists($user,$UserPassword)) $UserPassword[$user]=$pass;//和添加是不是有点类似
else echo "用户 $user 不存在!"
}else{
if ($UserPassword[$user]==$pass){
echo '密码正确。';
//接下来可能要做许多事情
}else echo '密码错误!';
}
//程序最后保存数据库修改
pwd_db_write();
?>

看得懂吗,没有上机调试,语法问题可能难免,如果发现不明白的问题请补充。

❻ php 如何实现在线预览文件如:txt,doc,pdf

第一种 预览

$file = fopen($path,"r"); // 打开文件
// 输入文件标签
Header("Content-type: application/pdf");
// Header("filename:" . $file_name);
// 输出文件内容
echo fread($file,filesize($path));
fclose($file);

第二种下载

Header("Content-type: application/pdf");// 文件将被称为 downloaded.pdf
header("Content-Disposition:attachment;filename='downloaded.pdf'");readfile($path);
第三种预览
Header("Content-type: application/pdf");// 文件将被称为 downloaded.pdf
header("Content-Disposition:inline;filename='downloaded.pdf'");readfile($path);
第四种下载
$file = fopen($path,"r"); // 打开文件
// 输入文件标签
Header("Content-type: application/octet-stream");
Header("Accept-Ranges: bytes");
Header("Accept-Length: ".filesize($path));
Header("Content-Disposition: attachment; filename=" . $file_name);
// 输出文件内容
echo fread($file,filesize($path));
fclose($file);

❼ php读取txt文本输出时乱码如何解决

$text = file_get_contents($filePath);
//$encodType = mb_detect_encoding($text);
define('UTF32_BIG_ENDIAN_BOM', chr(0x00) . chr(0x00) . chr(0xFE) . chr(0xFF));
define('UTF32_LITTLE_ENDIAN_BOM', chr(0xFF) . chr(0xFE) . chr(0x00) . chr(0x00));
define('UTF16_BIG_ENDIAN_BOM', chr(0xFE) . chr(0xFF));
define('UTF16_LITTLE_ENDIAN_BOM', chr(0xFF) . chr(0xFE));
define('UTF8_BOM', chr(0xEF) . chr(0xBB) . chr(0xBF));
$first2 = substr($text, 0, 2);
$first3 = substr($text, 0, 3);
$first4 = substr($text, 0, 3);
$encodType = "";
if ($first3 == UTF8_BOM)
$encodType = 'UTF-8 BOM';
else if ($first4 == UTF32_BIG_ENDIAN_BOM)
$encodType = 'UTF-32BE';
else if ($first4 == UTF32_LITTLE_ENDIAN_BOM)
$encodType = 'UTF-32LE';
else if ($first2 == UTF16_BIG_ENDIAN_BOM)
$encodType = 'UTF-16BE';
else if ($first2 == UTF16_LITTLE_ENDIAN_BOM)
$encodType = 'UTF-16LE';

//下面的判断主要还是判断ANSI编码的·
if ($encodType == '') {//即默认创建的txt文本-ANSI编码的
$content = iconv("GBK", "UTF-8", $text);
} else if ($encodType == 'UTF-8 BOM') {//本来就是UTF-8不用转换
$content = $text;
} else {//其他的格式都转化为UTF-8就可以了
$content = iconv($encodType, "UTF-8", $text);
}

❽ 如何用PHP读取TXT文件并且修改

/**
*读文件
**/
functionread_file($filename)
{
$fp=fopen($filename,"r")ordie("couldn'topen$filename");
$read=fread($fp,filesize($filename));
fclose($fp);
return$read;
}
/**
*写文件
**/
functionwrite_file($filename,$buffer)
{
$fp=fopen($filename,"w")ordie("couldn'topen$filename");
flock($fp,LOCK_EX);
$write=fputs($fp,$buffer);
flock($fp,LOCK_UN);
fclose($fp);
returntrue;
}
/**
*修改(只是追加内容)
**/
functionappend_to_file($filename,$buffer)
{
$fp=fopen($filename,"a")ordie("couldn'topen$filename");
flock($fp,LOCK_EX);
fputs($fp,$buffer);
flock($fp,LOCK_UN);
fclose($fp);
returntrue;
}
/**
*测试
**/
$str=read_file('test.txt');
echo$str;
write_file('test2.txt',$str);
append_to_file('test2.txt',"ABCD");

❾ 如何用PHP在网页对TXT文档查看和修改

<?php
//获取文本内容123
$content = file_get_contents("/website/aa.txt");
//查找localhsot,替换成您的IP地址
$str = str_replace("localhost","127.0.0.1",$content);
//以读写模式打开aa.txt文件
$file = fopen("/website/aa.txt","r+");
//将替换后的内容写入aa.txt文件中
fwrite($file,$str);
//关闭文件
fclose($file);
?>

阅读全文

与php打开txt相关的资料

热点内容
dvd光盘存储汉子算法 浏览:757
苹果邮件无法连接服务器地址 浏览:962
phpffmpeg转码 浏览:671
长沙好玩的解压项目 浏览:144
专属学情分析报告是什么app 浏览:564
php工程部署 浏览:833
android全屏透明 浏览:736
阿里云服务器已开通怎么办 浏览:803
光遇为什么登录时服务器已满 浏览:302
PDF分析 浏览:484
h3c光纤全工半全工设置命令 浏览:143
公司法pdf下载 浏览:381
linuxmarkdown 浏览:350
华为手机怎么多选文件夹 浏览:683
如何取消命令方块指令 浏览:349
风翼app为什么进不去了 浏览:778
im4java压缩图片 浏览:362
数据查询网站源码 浏览:150
伊克塞尔文档怎么进行加密 浏览:892
app转账是什么 浏览:163