Skip to content

Instantly share code, notes, and snippets.

@brunoalfred
Created March 4, 2022 06:37
Show Gist options
  • Select an option

  • Save brunoalfred/f3ad8b402420092ee04ccf76ed3dff6a to your computer and use it in GitHub Desktop.

Select an option

Save brunoalfred/f3ad8b402420092ee04ccf76ed3dff6a to your computer and use it in GitHub Desktop.
Function to perform file splitting in php.
<?php
/**
* A file splitter function for php
* Can split a file to number of parts depending on the buffer size given
*
* @param $file String
* Path of the file to split
* @param $buffer number
* The [maximum] size of the part of a file
* @return array S
* et of strings containing the paths to the parts
*/
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;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment