
$content = file_get_contents('testtxt');
$arr = explode("\n", $content);
echo "<table>";
foreach ($arr as $v) {
$tmp = explode(" ", $v);
echo "<tr>";
echo "<td>" $tmp[0] "</td>";
echo "<td>" $tmp[1] "</td>";
echo "<td>" $tmp[2] "</td>";
echo "<td>" $tmp[3] "</td>";
echo "</tr>";
unset($tmp);
}
echo "</table>";
首先,你的file控件要放到form表单内,其次,每个file控件的name属性应该不同。下面是我修改后的:
<html>
</head>
<script language="javascript" type="text/ecmascript">
var x=1;
//======================
//功能:在表单中input file控件
//参数:parentID---要插入input file控件的父元素ID
// inputID----input file控件的ID
//======================
function createInput(parentID,inputFileID){
var parent=$(parentID);//获取父元素
var div=documentcreateElement("div");//创建一个div容器用于包含input file
x++;
var divName=inputFileID+x;//随机div容器的名称
divid=divName;
var aElement=documentcreateElement("input"); //创建input
aElementname=divName;
aElementtype="file";//设置类型为file
var delBtn=documentcreateElement("input");//再创建一个用于删除input file的Button
delBtntype="button";
delBtnvalue="删除";
delBtnonclick=function(){ removeInput(parentID,divName)};//为button设置onclick方法
divappendChild(aElement);//将input file加入div容器
divappendChild(delBtn);//将删除按钮加入div容器
parentappendChild(div);//将div容器加入父元素
}
//============================
//功能:删除一个包含input file的div 容器
//参数:parentID---input file控件的父元素ID
// DelDivID----个包含input file的div 容器ID
//============================
function removeInput(parentID,DelDivID){
var parent=$(parentID);
parentremoveChild($(DelDivID));
}
//通过元素ID获取文档中的元素
function $(v){return documentgetElementById(v);}
</script>
<body>
<form action="testphp" method="post" enctype="multipart/form-data">
<div align="left" id="div_Pic" style="border:1px solid #CCCCCC">
<input name="PicFile" type="file" id="ShowPicFile">
</div>
<input type="button" onClick="createInput('div_Pic','PicFile')" name="button" id="button" value="+ 继续添加">
<input type="submit" value="提交">
</body>
</html>
然后php就可以通过遍历$_FILES来获得每个上传的文件。下面的简单例子只是列出每个文件的原文件名:
<php
forEach($_FILES as $f){
echo $f["name"]"<br>";
}
>
首先声明,我下面的代码是以你的那十行数据为基础,测试通过的。但是我得把它的局限说一下。
编码的时候,我是根据你的每一行的规律来的。每行用6个数据分隔7个部分的内容,所以,每个部分不能再有逗号了(当然这个危险主要来自标题,不过我看你标题分隔的时候都是空格或!)
你直接运行吧!
<PHP
$file_name="datatxt"; //假设你的数据是存在这个文件中的
$fp=fopen($file_name,'r');
while(!feof($fp)) //文件全部要读完
{
$buffer=fgets($fp,1024); //获得一行
$period = explode(",",$buffer); //以逗号分隔分行内容
if($period[6]!=1){ //找到第7部分的内容,如果不是1,就输出当然这个也是开关,你可以设置为1时输出
echo $buffer"<br>";
}
}
fclose($fp); //关闭文件流
>
php读取文件内容:
-----第一种方法-----fread()--------
<php
$file_path = "testtxt";
if(file_exists($file_path)){
$fp = fopen($file_path,"r");
$str = fread($fp,filesize($file_path));//指定读取大小,这里把整个文件内容读取出来
echo $str = str_replace("\r\n","<br />",$str);
}
>
--------第二种方法------------
<php
$file_path = "testtxt";
if(file_exists($file_path)){
$str = file_get_contents($file_path);//将整个文件内容读入到一个字符串中
$str = str_replace("\r\n","<br />",$str);
echo $str;
}
>
-----第三种方法------------
<php
$file_path = "testtxt";
if(file_exists($file_path)){
$fp = fopen($file_path,"r");
$str = "";
$buffer = 1024;//每次读取 1024 字节
while(!feof($fp)){//循环读取,直至读取完整个文件
$str = fread($fp,$buffer);
}
$str = str_replace("\r\n","<br />",$str);
echo $str;
}
>
-------第四种方法--------------
<php
$file_path = "testtxt";
if(file_exists($file_path)){
$file_arr = file($file_path);
for($i=0;$i<count($file_arr);$i++){//逐行读取文件内容
echo $file_arr[$i]"<br />";
}
/
foreach($file_arr as $value){
echo $value"<br />";
}/
}
>
----第五种方法--------------------
<php
$file_path = "testtxt";
if(file_exists($file_path)){
$fp = fopen($file_path,"r");
$str ="";
while(!feof($fp)){
$str = fgets($fp);//逐行读取。如果fgets不写length参数,默认是读取1k。
}
$str = str_replace("\r\n","<br />",$str);
echo $str;
}
>
以上就是关于php读取文本文件内容~全部的内容,包括:php读取文本文件内容~、JS动态创建的file控件,PHP 怎么获取上传的文件、如何用php读取txt文件里面的单行数据等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)