PHP example of how to merge the contents of multiple files into a new file

不言
Release: 2023-03-24 21:40:02
Original
1442 people have browsed it

This article mainly introduces PHP's method of merging the contents of multiple files into new files, involving PHP encoding conversion, file and directory traversal, and file reading and writing related operating techniques. Friends in need can refer to the following

The example in this article describes how PHP implements merging the contents of multiple files into a new file. Share it with everyone for your reference, the details are as follows:


function test()
{
  $hostdir= iconv("utf-8","gbk","C:\Users\原万里\Desktop\日常笔记") ; //iconv()转换编码方式,将UTF-8转换为gbk,若是报错在gbk后加//IGNORE
  $filesnames = scandir($hostdir); //scandir() 函数返回指定目录中的文件和目录的数组。默认升序排列,
  foreach ($filesnames as $name) {
       if($name!=".." && $name!=".") //遍历结果中会多出‘.'以及‘..',没有用处,不予处理;
       {
         $cipath = $hostdir.$name;
         $cjfilenames = scandir($cipath); //根据情况再决定是否再向下遍历一次
         foreach($cjfilenames as $cjname)
         {
            if($cjname!=".." && $cjname!="." &&!is_dir($cipath."/".$cjname))
             {
               $str = file_get_contents($cipath."/".$cjname);
               file_put_contents("e:/test.txt",$str,FILE_APPEND);
             }
         }
       }
     echo "\n";
  }
}
Copy after login

##Related recommendations:

Using ajax php to implement file upload

PHP implementation of obtaining file mime type tool class example_php skills

The above is the detailed content of PHP example of how to merge the contents of multiple files into a new file. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!