我想从php代码中拆分多个部分的大文件(具体来说,tar.gz文件)。这样做的主要原因是,php对32位系统的2gb限制。
所以我想分割多个部分的大文件并单独处理每个部分。
这可能吗?如果是,怎么样?
答案 0 :(得分:9)
我的评论被投了两次,所以也许我的猜测是:P
如果在unix环境中,请尝试此操作...
exec('split -d -b 2048m file.tar.gz pieces');
您的作品应为pieces1
,pieces2
等
您可以通过在PHP中使用stat()
来获取文件大小,然后执行简单的数学(int) ($stat['size'] / 2048*1024*1024)
(我认为),轻松获得结果片段的数量。
答案 1 :(得分:8)
一个简单的方法(如果使用基于Linux的服务器)是使用exec
命令并运行split
命令:
exec('split Large.tar.gz -b 4096k SmallParts'); // 4MB parts
/* | | | | |
| | |______| |
App | | |_____________
The source file | |
The split size Out Filename
*/
请点击此处了解详情:http://www.computerhope.com/unix/usplit.htm
或者您可以使用:http://www.computerhope.com/unix/ucsplit.htm
exec('csplit -k -s -f part_ -n 3 LargeFile.tar.gz');
PHP在单个线程中运行,增加此线程数的唯一方法是使用fork
命令创建子进程。
这不是资源友好的。我建议的是研究一种能够快速有效地完成这项工作的语言。我建议使用node.js。
只需在服务器上安装节点,然后创建一个名为node_split
的小脚本,它可以为您自己完成这项工作。
但我强烈建议您不要将PHP用于此作业,而是使用exec来允许主机操作系统执行此操作。
答案 2 :(得分:3)
HJSPLIT
答案 3 :(得分:1)
PHP本身可能无法......
如果您可以从计算机的命令行中找出如何执行此操作,
您应该能够使用exec();
答案 4 :(得分:1)
function split_file($source, $targetpath='/split/', $lines=1000){
$i=0;
$j=1;
$date = date("m-d-y");
$buffer='';
$handle = fopen ($_SERVER['DOCUMENT_ROOT'].$source, "r");
while (!feof ($handle)) {
$buffer .= fgets($handle, 4096);
$i++;
if ($i >= $lines) {
$fname = $_SERVER['DOCUMENT_ROOT'].$targetpath."part_".$date.$j.".txt";
$fhandle = fopen($fname, "w") or die($php_errormsg);
if (!$fhandle) {
echo "Cannot open file ($fname)";
//exit;
}
if (!fwrite($fhandle, $buffer)) {
echo "Cannot write to file ($fname)";
//exit;
}
fclose($fhandle);
$j++;
$buffer='';
$i=0;
$line+=10; // add 10 to $lines after each iteration. Modify this line as required
}
}
fclose ($handle);
}
答案 5 :(得分:1)
$handle = fopen('source/file/path','r');
$f = 1; //new file number
while(!feof($handle))
{
$newfile = fopen('newfile/path/'.$f.'.txt','w'); //create new file to write to with file number
for($i = 1; $i <= 5000; $i++) //for 5000 lines
{
$import = fgets($handle);
//print_r($import);
fwrite($newfile,$import);
if(feof($handle))
{break;} //If file ends, break loop
}
fclose($newfile);
$f++; //Increment newfile number
}
fclose($handle);
答案 6 :(得分:0)
答案 7 :(得分:0)
这可能在php中可行,但是php是为Web开发而构建的,并且在一个请求中尝试整个操作将导致请求超时。
然而,你可以使用另一种语言,如java或c#,并构建一个后台进程,你可以通过php通知它来执行操作。甚至可以从php运行,具体取决于主机上的安全设置。
答案 8 :(得分:0)
Splits命名为filename.part0 filename.part1 ...
<?php
function fsplit($file,$buffer=1024){
//open file to read
$file_handle = fopen($file,'r');
//get file size
$file_size = filesize($file);
//no of parts to split
$parts = $file_size / $buffer;
//store all the file names
$file_parts = array();
//path to write the final files
$store_path = "splits/";
//name of input file
$file_name = basename($file);
for($i=0;$i<$parts;$i++){
//read buffer sized amount from file
$file_part = fread($file_handle, $buffer);
//the filename of the part
$file_part_path = $store_path.$file_name.".part$i";
//open the new file [create it] to write
$file_new = fopen($file_part_path,'w+');
//write the part of file
fwrite($file_new, $file_part);
//add the name of the file to part list [optional]
array_push($file_parts, $file_part_path);
//close the part file handle
fclose($file_new);
}
//close the main file handle
fclose($file_handle);
return $file_parts;
}
?>