You need a PHP merge script, which you upload together with the split parts to the FTP server to recombine all the split parts to recreate the original file. As discussed before, this is a PHP web form that accepts two important user inputs for merging, the file name and the number of split parts. The file name should be the exact name with the exact file name extension. The concept and structure of form HTML code is similar to the PHP file split script. Once it is completed, the initial step is to receive the POST data: $merged_file_name =trim($_POST['filename']); $parts_num =trim($_POST['parts']); After receiving the data and assigning it to PHP variables, we are now ready to make the merge PHP function. We start by defining an empty container for the content: $content=' '; The next crucial step is to read the split parts (i.e. splited_0, splited_1) based on the split part file size and then assign it to the $content variable. Since there is more than one part, PHP loops (like FOR loops) are employed to efficiently read the file. for($i=0;$i<$parts_num;$i++) { $file_size = filesize('splited_'.$i); $handle = fopen('splited_'.$i, 'rb') or die("error opening file"); $content .= fread($handle, $file_size) or die("error reading file"); } Once the file has been read and assigned to the $content variable, we need to make a new file using the exact file name (as it was entered in the PHP file merge web form). This is a file creating process using the fopen PHP function command. $handle=fopen($merged_file_name, 'wb') or die("error creating/opening merged file"); Once the file has been created, all parts of the file will be safely combined in a file writing process using the PHP fwrite command. fwrite($handle, $content) or die("error writing to merged file"); The final PHP merging function is this: function merge_file($merged_file_name,$parts_num) { $content=''; //put splited files content into content for($i=0;$i<$parts_num;$i++) { $file_size = filesize('splited_'.$i); $handle = fopen('splited_'.$i, 'rb') or die("error opening file"); $content .= fread($handle, $file_size) or die("error reading file"); } //write content to merged file $handle=fopen($merged_file_name, 'wb') or die("error creating/opening merged file"); fwrite($handle, $content) or die("error writing to merged file"); return 'OK'; } Once this function has been included in the file merging script, the function can be called at the last part of the PHP script: merge_file($merged_file_name,$parts_num) or die('Error merging files');
blog comments powered by Disqus |
|
|
|
|
|
|
|