
正文
php读文件数据 php读取文件内容的方法和函数
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
PHP如何读出当前目录下所有文件?
一般来说php中读取目录下的文件名的方式确实不少,最简单的是scandir,具体代码如下:\x0d\x0a复制代码 代码如下:$dir="./caxa/";\x0d\x0a$file=scandir($dir);\x0d\x0aprint_r($file);\x0d\x0a稍微复杂点的,来自于php手册:\x0d\x0a复制代码 代码如下:$dir = "/etc/php5/";\x0d\x0a// Open a known directory, and proceed to read its contents\x0d\x0aif (is_dir($dir)) {\x0d\x0aif ($dh = opendir($dir)) {\x0d\x0awhile (($file = readdir($dh)) !== false) {\x0d\x0aecho "filename: $file : filetype: " . filetype($dir . $file) . "\n";\x0d\x0a} closedir($dh);\x0d\x0a}\x0d\x0a}\x0d\x0a这些都只能读取当前指定目录下的文件,对子目录中的文件则无法读取。原来自己写过一个循环删除所有目录的一段代码,需要逐个子目录删除所有文件,包括多层。但是只需要读出文件名,稍微复杂点,网上找到一个能用,原始代码有错误提示,改了一下引用$data的地方,如下所示:\x0d\x0a复制代码 代码如下:function searchDir($path,$data){\x0d\x0aif(is_dir($path)){\x0d\x0a$dp=dir($path);\x0d\x0awhile($file=$dp-read()){\x0d\x0aif($file!='.' $file!='..'){\x0d\x0asearchDir($path.'/'.$file,$data);\x0d\x0a}\x0d\x0a}\x0d\x0a$dp-close();\x0d\x0a}\x0d\x0aif(is_file($path)){\x0d\x0a$data[]=$path;\x0d\x0a}\x0d\x0a}\x0d\x0afunction getDir($dir){\x0d\x0a$data=array();\x0d\x0asearchDir($dir,$data);\x0d\x0areturn $data;\x0d\x0a}\x0d\x0aprint_r(getDir('.'));\x0d\x0a希望本文所述对大家的PHP程序设计有所帮助。
相关问答
Q1: PHP读取文件内容
把文本里面的内容改成
p style="color:red"you did not/p
pstrong Your order could not be processed at this time.Please try again later./strong/p
?php echo date('H:i,jS F Y'); ?
试试
Q2: PHP如何写入,读取与替换文件内容
$content = file_get_contents($file); //读文件
$content = $content . '正在修改'; //修改文件
file_put_contents($file, $content); //保存文件
Q3: php 读写文件和数据库哪个快
1、直接读文件相比数据库查询效率更胜一筹,而且文中还没算上连接和断开的时间。
2、一次读取的内容越大,直接读文件的优势会越明显(读文件时间都是小幅增长,这跟文件存储的连续性和簇大小等有关系),这个结果恰恰跟天缘预料的相反,说明MYSQL对更大文件读取可能又附加了某些操作(两次时间增长了近30%),如果只是单纯的赋值转换应该是差异偏小才对。
3、写文件和INSERT几乎不用测试就可以推测出,数据库效率只会更差。
4、很小的配置文件如果不需要使用到数据库特性,更加适合放到独立文件里存取,无需单独创建数据表或记录,很大的文件比如图片、音乐等采用文件存储更为方便,只把路径或缩略图等索引信息放到数据库里更合理一些。
5、PHP上如果只是读文件,file_get_contents比fopen、fclose更有效率,不包括判断存在这个函数时间会少3秒左右。
6、fetch_row和fetch_object应该是从fetch_array转换而来的,我没看过PHP的源码,单从执行上就可以说明fetch_array效率更高,这跟网上的说法似乎相反。
Q4: php怎么读取txt文本内容存入mysql数据库
第一步,读取txt的文件。假设为a.txt
$content = file_get_content('a.txt'); //读取文件内容存入变量。
第二步,存入数据库
mysql_query("insert 表名 (字段名) values('".$content."'));
Ps:文件是上传的,上传后的临时文件名是:$_FILE['tmp_name']
Q5: php如何读取文本指定的内容?
php读取文件内容:
-----第一种方法-----fread()--------
?php
$file_path = "test.txt";
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 = "test.txt";
if(file_exists($file_path)){
$str = file_get_contents($file_path);//将整个文件内容读入到一个字符串中
$str = str_replace("\r\n","br /",$str);
echo $str;
}
?
-----第三种方法------------
?php
$file_path = "test.txt";
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 = "test.txt";
if(file_exists($file_path)){
$file_arr = file($file_path);
for($i=0;$icount($file_arr);$i++){//逐行读取文件内容
echo $file_arr[$i]."br /";
}
/*
foreach($file_arr as $value){
echo $value."br /";
}*/
}
?
----第五种方法--------------------
?php
$file_path = "test.txt";
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读取文件内容的方法和函数的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。








