Home arrow PHP arrow Page 3 - Merging a File Split for FTP Upload using PHP

The File Merging Script - PHP

This is the second part of a tutorial on file splitting during an FTP upload. In this part, you will learn the details of file merging and implementation. The split PHP script was thoroughly discussed in the first part. It is highly recommended that you read that part to easily understand this one.

TABLE OF CONTENTS:
  1. Merging a File Split for FTP Upload using PHP
  2. Finding the file to be split
  3. The File Merging Script
  4. Implementation of the File Merging Script
By: Codex-M
Rating: starstarstarstarstar / 3
November 23, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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');



 
 
>>> More PHP Articles          >>> More By Codex-M
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: