Wednesday, October 30, 2013

Splitting a file into parts using PHP

Heyy folks,

A quick guide here. Recently i had to implement a file spliter in php and was shocked find how LESS pre written scripts are available for the task. So thought of writing a quick one myself.

If you just want the code, i'have a fully featured php script hosted at github
Just download it there and well CONTRIBUTE :)


1) Take the URL of the base file [we donot write the file upload part]
2) Take the size of a part of the file [lets call it buffer]
3) Read a buffer size of amount from the file till end-of-file is reached
4) For every read part, write it as a seperate file
5) For clarity the naming of files go like input_file_name.part<no>
     ex: myawesomefile.png.part0 myawesomefile.png.part1



  1. <?php
  2. /**
  3.  * A file splitter function for php
  4.  * Can split a file to number of parts depending on the buffer size given
  5.  *
  6.  * @author manujith pallewatte []
  7.  * @date   30/10/13
  8.  *  *
  9.  * @param  $file String
  10.  * Path of the file to split
  11.  * @param $buffer number
  12.  * The [maximum] size of the part of a file
  13.  * @return array S
  14.  * et of strings containing the paths to the parts
  15.  */
  16. function fsplit($file,$buffer=1024){
  17.     //open file to read
  18.     $file_handle = fopen($file,'r');
  19.     //get file size
  20.     $file_size = filesize($file);
  21.     //no of parts to split
  22.     $parts = $file_size / $buffer;
  24.     //store all the file names
  25.     $file_parts = array();
  27.     //path to write the final files
  28.     $store_path = "splits/";
  30.     //name of input file
  31.     $file_name = basename($file);
  33.     for($i=0;$i<$parts;$i++){
  34.         //read buffer sized amount from file
  35.         $file_part = fread($file_handle, $buffer);
  36.         //the filename of the part
  37.         $file_part_path = $store_path.$file_name.".part$i";
  38.         //open the new file [create it] to write
  39.         $file_new = fopen($file_part_path,'w+');
  40.         //write the part of file
  41.         fwrite($file_new, $file_part);
  42.         //add the name of the file to part list [optional]
  43.         array_push($file_parts, $file_part_path);
  44.         //close the part file handle
  45.         fclose($file_new);
  46.     }    
  47.     //close the main file handle
  49.     fclose($file_handle);
  50.     return $file_parts;
  51. }
  53. ?>

Most of the code is documented but i'll get into some important areas
First few lines are about taking in the filename, extracting the basename [thefile.exe part]

Next we find the number of parts to make
no_of_parts = size_of_file / buffer_size

Then we loop through 0 - no_of_parts
in each loop we read a portion of the input file. Now the question is why php wont read the same portion always.
It's simple, because when reading a file php keeps a pointer as to well where it is reading now. Thus when we tell it to read the second time, it simply start to read from the point it stopped previously :)
Convenient huh?

So DONE! You now have a complete splitter written in php and you are free to carry it anywhere


No comments:

Post a Comment