以下是一个简单的PHP脚本示例,用于合并多个文件到一个文件中。这个例子中,我们将合并指定目录下的所有`.txt`文件到一个名为`merged.txt`的文件中。

```php

实例php文件合并,实例PHP文件合并:如何使用PHP脚本合并多个文件  第1张

// 设置源文件目录和目标文件路径

$sourceDir = 'path/to/source/directory';

$targetFile = 'path/to/merged.txt';

// 打开目标文件准备写入

$targetHandle = fopen($targetFile, 'w') or die('Cannot open target file.');

// 遍历源目录下的所有文件

$files = glob($sourceDir . '/*.txt');

foreach ($files as $file) {

// 打开源文件准备读取

$sourceHandle = fopen($file, 'r') or die('Cannot open source file.');

// 读取源文件内容并写入目标文件

while (!feof($sourceHandle)) {

$line = fgets($sourceHandle);

fwrite($targetHandle, $line);

}

// 关闭源文件

fclose($sourceHandle);

}

// 关闭目标文件

fclose($targetHandle);

echo "