php如何读取文本指定的内容

php如何读取文本指定的内容,第1张

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;

}

>

首先,你的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>";

}

>

<php

$dir = "/images/"; //要获取的目录

echo " 获取目录下所有文件和文件夹 <hr/>";

//先判断指定的路径是不是一个文件夹

if (is_dir($dir)){

if ($dh = opendir($dir)){

while (($file = readdir($dh))!= false){

//文件名的全路径 包含文件名

$filePath = $dir$file;

echo "<img src='"$filePath"'/>";

}

closedir($dh);

}

}

>

以上就是关于php如何读取文本指定的内容全部的内容,包括:php如何读取文本指定的内容、JS动态创建的file控件,PHP 怎么获取上传的文件、php怎么获取文件夹内的所有图片并且显示出来等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/10147178.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-05
下一篇 2023-05-05

发表评论

登录后才能评论

评论列表(0条)

保存