在 PHP 中连接文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17407598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Concatenate files in PHP
提问by JorgeeFG
I'd like to know if there is a faster way of concatenating 2 text files in PHP, than the usual way of opening txt1
in a+
, reading txt2
line by line and copying each line to txt1
.
我想知道是否有在PHP串联2个文本文件的一个更快的方法,不是打开的常用方法txt1
中a+
,读出txt2
一行行并复制每一行txt1
。
回答by Patrick
It's probably much faster to use the cat
program in linux if you have command line permissions for PHP
cat
如果您有 PHP 的命令行权限,在 linux 中使用该程序可能要快得多
system('cat txt1 txt2 > txt3');
回答by Bart Friederichs
If you want to use a pure-PHP solution, you could use file_get_contents
to read the whole file in a string and then write that out (no error checking, just to show how you could do it):
如果您想使用纯 PHP 解决方案,您可以使用file_get_contents
字符串读取整个文件,然后将其写出(没有错误检查,只是为了展示您如何做到这一点):
$fp1 = fopen("txt1", 'a+');
$file2 = file_get_contents("txt2");
fwrite($fp1, $file2);
回答by Blackfire
$content = file_get_contents("file1");
file_put_contents("file2", $content, FILE_APPEND);
回答by lufc
I have found using *nix cat
to be the most effective here, but if for whatever reason you don't have access to it, and you are concatenating large files, then you can use this line by line function. (Error handling stripped for simplicity).
我发现cat
在这里使用 *nix是最有效的,但是如果由于某种原因您无法访问它,并且您正在连接大文件,那么您可以使用此逐行功能。(为简单起见,删除了错误处理)。
function catFiles($arrayOfFiles, $outputPath) {
$dest = fopen($outputPath,"a");
foreach ($arrayOfFiles as $f) {
$FH = fopen($f,"r");
$line = fgets($FH);
while ($line !== false) {
fputs($dest,$line);
$line = fgets($FH);
}
fclose($FH);
}
fclose($dest);
}
回答by FrancescoMM
While the faster way is using OS commands like cp or cat, this is hardly advisable for compatibility.
虽然更快的方法是使用 cp 或 cat 等操作系统命令,但为了兼容性,这几乎是不可取的。
The fastest "PHP only" way is using file_get_contents, that reads the whole source file, in one shot but it also has some drawbacks. It will require a lot of memory for large files and for this reason it may fail depending on the memory assigned to PHP.
最快的“仅限 PHP”方法是使用 file_get_contents,它一次性读取整个源文件,但它也有一些缺点。大文件需要大量内存,因此它可能会失败,具体取决于分配给 PHP 的内存。
A universal clean and fast solution is to use fread and fwrite with a large buffer.
一个通用的干净和快速的解决方案是使用 fread 和 fwrite 和一个大缓冲区。
If the file is smaller than the buffer, all reading will happen in one burst, so speed is optimal, otherwise reading happens at big chunks (the size of the buffer) so the overhead is minimal and speed is quite good.
如果文件小于缓冲区,所有读取将在一次突发中发生,因此速度是最佳的,否则读取发生在大块(缓冲区的大小)中,因此开销最小且速度非常好。
Reading line by line with fgets instead, has to test for every charachter, one by one, if it's a newline or line feed. Also, reading line by line with fgets a file with many short lines will be slower as you will read many little pieces, of different sizes, depending of where newlines are positioned.
相反,使用 fgets 逐行读取,必须逐个测试每个字符,如果它是换行符或换行符。此外,使用 fgets 逐行读取包含许多短行的文件会更慢,因为您将读取许多不同大小的小块,具体取决于换行符的位置。
fread is faster as it only checks for EOF (which is easy) and reads files using a fixed size chunk you decide, so it can be made optimal for your OS or disk or kind of files (say you have many files <12k you can set the buffer size to 16k so they are all read in one shot).
fread 更快,因为它只检查 EOF(这很容易)并使用您决定的固定大小块读取文件,因此它可以优化您的操作系统或磁盘或文件类型(假设您有许多文件 <12k,您可以将缓冲区大小设置为 16k,以便一次性读取它们)。
// Code is untested written on mobile phone inside Stack Overflow, comes from various examples online you can also check.
// 代码是在Stack Overflow里面用手机写的未经测试,来自网上的各种例子你也可以查看。
<?php
$BUFFER_SIZE=1*1024*1024; // 1MB, bigger is faster.. depending on file sizes and count
$dest = fopen($fileToAppendTo "a+");
if (FALSE === $dest) {
exit("Failed to open destination");
}
$handle = fopen("source.txt", "rb");
if (FALSE === $handle) {
exit("Failed to open source");
}
$contents = '';
while (!feof($handle)) {
$contents = fread($handle, $BUFFER_SIZE);
fwrite($dest,$contents);
fclose($handle);
?>