我想从PHP代码中分割出大量文件(具体来说,tar.gz文件)。这样做的主要原因是,在32位系统上的php的2GB限制。使用PHP分割大文件
因此,我想分割多个部分的大文件,并分别处理每个部分。
这可能吗?如果是,如何?
我想从PHP代码中分割出大量文件(具体来说,tar.gz文件)。这样做的主要原因是,在32位系统上的php的2GB限制。使用PHP分割大文件
因此,我想分割多个部分的大文件,并分别处理每个部分。
这可能吗?如果是,如何?
PHP本身可能无法... 如果你能弄清楚如何从你电脑的命令行, 做到这一点你应该能够然后使用exec();
高管可能对很多共享主机被禁用,因此不会为我工作 – Mihir 2011-03-23 13:16:00
此执行这些命令可能在PHP中可能,但PHP是为Web开发构建的,尝试在一个请求中执行整个操作会导致请求超时。
然而,你可以使用另一种语言,如java或c#,并建立一个后台进程,你可以通知从PHP执行操作。甚至可以从php运行,具体取决于主机上的安全设置。
我的评论被选为了两次,所以也许我的猜测是在东西:P
如果在UNIX环境中,试试这个...
exec('split -d -b 2048m file.tar.gz pieces');
Your piec ES应该pieces1
,pieces2
等
你可以得到通过在PHP中使用stat()
得到文件的大小容易产生碎片的数量,然后做简单的数学(int) ($stat['size']/2048*1024*1024)
(我认为)。
非常有趣的..绝对值得寻找更多... +1的概念 – 2018-01-13 17:37:03
一个简单的方法(如果使用基于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来允许主机操作系统执行此操作。
执行可能会被禁用的许多共享主机,所以不会为我工作 – Mihir 2011-03-23 13:16:43
这是我的假设,如果他在共享主机,他不会有这样的服务器上的大文件。 – RobertPitt 2011-03-23 13:26:50
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);
}
$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);
拆分被命名为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;
}
?>
这是*不*对PHP的任务。你在做什么操作系统? – 2011-03-22 12:37:35
'使用'exec()'split -b 2048 m file.tar.gz pieces'? – alex 2011-03-22 12:38:50
@alex非常有趣.... – 2018-01-13 17:33:51