Merging a File Split for FTP Upload using 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.

As discussed before, once all the split parts of the file have been uploaded to the FTP server, you can safely recombine them into one file again (this is the merging process) using the PHP file merging script.

Example: File splitting process

Before we start the merging process, let’s look at an actual example of the file splitting process.

Once the PHP File splitting script (splitthisfile.php) has been completed, you are ready to split your file by entering the file name and the FTP hosting server’s maximum file upload limit.

You can download the splitthisfile.php script. It was fully explained in the first part.

Make a folder named “splitfolder” and place it in the local host root directory (path: C:xampphtdocssplitfolder). Within this folder, place the “splitthisfile.php.” Using a browser, type the following URL:

http://localhost/splitfolder/splitthisfile.php

For example, if the file name is “test.mp3” and the upload limit is 500 KB, enter these details in the form; it should be entered as in the screenshot below:

{mospagebreak title=Finding the file to be split}

Sometimes it is difficult to detect the exact file name and extension. In that case you will need to use the Windows DOS command prompt and navigate to the XAMPP htdocs directory.

For example, if the file to be split is located in C:xampphtdocssplitfolder in your Windows-based operating system, go to Start -> Run -> and type CMD in the DOS prompt. After that, type EACH of the following commands to navigate to the split folder and then press ENTER:

CD (press enter)

CD XAMPP (press enter)

CD htdocs (press enter)

CD splitfolder (press enter)

DIR (press enter)

At the end of this command, you will see the list of files inside the “splitfolder” directory:

It confirms that the exact file name and extension is “test.mp3.”

Use this method to find the exact file name and file name extension.

After everything is set, click the button labeled “Split this large file into parts which is less than file upload limit.”

The file is automatically split into parts, and this will be displayed on the form. You will get a message telling you that the file has been split successfully. You will also be given details which you’ll need to use when you merge the files. In our example, the filename to be used for merging should be test.mp3, and the number of parts after the split should be 12.

The script gave the 12 parts that were created by the split the filenames  splited_0, splited_1, splited_2, etc. These can be found in the split folder directory. At this stage, the file splitting process is complete.

{mospagebreak title=The File Merging Script}

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

{mospagebreak title=Implementation of the File Merging Script}

To implement this file merging script you need to upload it to the FTP server. You can download the complete PHP file merging script. Or copy and paste the code in the previous section to a notepad file and save as “mergethisfile.php.”

Below are the steps necessary to implement the file merging process:

Step 1: Create a folder name “merge.” This folder can be placed in your desktop.

Step 2: Copy all the split parts (created in XAMPP local host folder) to this folder. If you have 12 parts (those with file name, splited_0, splited_1, etc as shown in the previous example), copy them to this folder.

Step 3: Copy the “mergethisfile.php” to the merge folder.

Step 4: Open your favorite FTP client. Upload each part (the split parts and the FTP folder) to the desired location in the FTP server. If there are 12 parts, there will be 12 upload sessions, with each upload size at 258 KB (using the example above; each part is less than the 500 KB maximum size upload limit).

For example, I would like to upload this inside the “mp3” directory. The screenshot below shows an example of the file parts and the mergethisfile.php script after uploading in the FTP server. It also shows the screenshot after merging and files parts deletion (details below):

Step 5: In your browser address bar, type the path of the mergethisfile.php and press enter. Here is an example (using the illustrations above):

http://www.php-developer.org/mp3/mergethisfile.php

A web form will be shown. Type the details as accurately as possible. Since test.mp3 (the example in this article) was split into 12 parts (see results in the previous section), you need to enter 12 in the “Enter the number of splitted parts” field; see screenshot below:

After filling in the information, click “Merge this file.”

The merged file will now be created in the directory, and you can safely delete the parts as well as the merge script. The result is a single file, which is “test.mp3” (see related screenshots above).

[gp-comments width="770" linklove="off" ]
antalya escort bayan antalya escort bayan