我想從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