How to Split a File During an FTP Upload Using PHP

One of the known limitations of free hosting packages is the file upload limit. For example, some hosting companies set an upload limit of 500 KB. This means that for any uploads to the FTP server, the file should not be more than 500 KB or else the server won’t accept it and you will not be able to upload your file. Fortunately, by splitting your files, you can get around this limitation; this two-part tutorial series will show you how.

Of course, some files that are important for web site functionality exceed 500 KB. For example, if you are running a web application that needs a geo IP database, a typical file for the geo IP DAT file is around 1.1MB. So this cannot be uploaded as one batch because it exceeds the upload limit of 500 KB. Another common example is uploading music or videos such as MP3, wav or .WMV files.

There is no easy way to break down a file like this (especially if the web hosting company does not allow uploading of archives like .zip, .rar), and technical solutions are needed to upload this file as a single batch without switching to another hosting agency, which can be inefficient in some cases.

As mentioned in the introduction, this is the first part of a two-part tutorial series. In this part, you will learn how to create the PHP split file script.

Concept of Implementation

To provide a glimpse of how you are going to implement the splitting and merging of files, see the flowchart below:

You need XAMPP installed in your computer. If you are using a Windows-based operating system, you can read a tutorial that will help guide you through the installation process. 

The "split folder" directory contains the "PHP File splitting script" which will be discussed in this tutorial. Also, the script is a web form which will ask for the name of the file as well as the FTP server maximum file upload limit in kilobytes. You can get this data from your web hosting agency. Make sure the data is in kilobytes (KB).

Once the split file script has been executed by submitting the form, several parts of the file will be created. This means that the file will be split into "N" parts based on the file size and the upload limit. Details of this process will be covered later.

After the file has been split successfully, it can be uploaded easily to your hosting FTP server because it is now less than the maximum file upload limit. You are going to upload it to the desired location in your FTP server.

Along with the file to be uploaded is the "PHP merge script," which is also a web form that asks for merge file settings, such as your exact file name and the number of parts into which the file has been split (these will be discussed thoroughly in the second part of this series).

After executing the file merging script, the parts of the split files will be combined into one (back to the original file, but this time in the FTP server, NOT in your local host directory). The remnants or remains of the split parts, however, will still be there in your FTP server. In that case, you can simply delete those fragments after successful file merging.

It is also safe to delete the PHP merge script once the merging has been completed to prevent unauthorized use.

{mospagebreak title=Inside the Split Folder Directory}

The split file directory should consist of two important files before file splitting process:

  • The PHP file splitting script (named "splitthisfile.php" in this tutorial).
  • The file to be split.

After the file is split, it will be broken down into chunks or fragments. See the screenshot of the file changes inside the "split file" directory after the split. By the way, if you are using Windows and have installed XAMPP in the root of drive C, the path of "split file" directory should be:

C:xampphtdocssplitfolder

Based on the above screenshot, the file to be split is "GeoIP.dat." You can split any type of large file.

{mospagebreak title=PHP File Splitting Script: splitthisfile.php}

The first step is to show a web form that accepts the file name to be split and the maximum file upload limit size in kilobytes. The second step should be to receive the POST data and compute the number of splitting parts. (The complete script will be downloaded/shown in the second part of this tutorial)

The formula for the number of parts into which the file will be split is:

Number of Split Parts (PHP variable: $parts_num) = (File size / Maximum Upload limit) x 2

The script uses a safety factor of two to make sure the resulting file size of the split parts is significantly less than the maximum upload limit of the FTP server. So for example, say the file size to be split is 1.1 MB and the FTP server maximum upload size is 500KB. The number of split parts can be computed like this:

Number of Split Parts (PHP variable: $parts_num) = ((1100KB) / (500KB)) x 2 = 4.4, rounding down to implement a realistic value brings us to four parts.

Since 1.1MB cannot be uploaded at once using a 500 KB upload limit, with four parts the file size per upload will be reduced to 1100KB/4 ~ 275KB, which is less than 500KB, but you have to upload each of the parts to the FTP server.

In PHP the file size can be measured by:

$filesize = ((filesize($file_name))/1000);

Using the filesize() function gives the results in bytes, so to convert that to kilobytes, divide the equation by 1000.

Rounding down can be accomplished by a floor() PHP function, so for example to implement rounding down of the number of parts:

$parts_num = floor($parts_num);

Then the split file function (PHP user-defined function splitthisfile()) accepts two inputs, $file_name and $parts_num. A modification script was formulated based on Arash Hemmat’s original PHP class.

{mospagebreak title=Using the File Splitting Function}

The initial steps in this function are to create a handle to open the file, define a file size to be used in the function (in bytes), and define the part size:

$handle = fopen($file_name, ‘rb’) or die("error opening file");

$file_size =filesize($file_name);

$parts_size = floor($file_size/$parts_num);

$modulus=$file_size % $parts_num;

fopen uses ‘rb’ because it needs to only read the file in binary mode, which does not alter or translate the original data preserving its type.

$part_size is a variable that contains the whole number of parts (round down using the floor function). If there is a remainder, it is assigned to the $modulus variable.

Once the file is open, PHP will need to read the file according to the set part size and modulus.

for($i=0;$i<$parts_num;$i++)

{

if($modulus!=0 & $i==$parts_num-1)

$parts[$i] = fread($handle,$parts_size+$modulus) or die("error reading file");

else

$parts[$i] = fread($handle,$parts_size) or die("error reading file");

}

//close file handle

fclose($handle) or die("error closing file handle");

The reading process is a loop that iterates based on the number of split parts ($parts_num). Once the file has been read, you need to write the file (this will actually create the parts of the split file).

for($i=0;$i<$parts_num;$i++)

{

$handle = fopen(‘splited_’.$i, ‘wb’) or die("error opening file for writing");

fwrite($handle,$parts[$i]) or die("error writing splited file");

}

//close file handle

fclose($handle) or die("error closing file handle");

return ‘OK';

}

It still iterates based on the split parts ($parts_num). And now the file created will start with ‘splited_’, and will be placed in the same folder as splitthisfile.php is located. (See previous screenshots)

The final splitthisfile() function will be:

function splitthisfile($file_name,$parts_num)

{

$handle = fopen($file_name, ‘rb’) or die("error opening file");

$file_size =filesize($file_name);

$parts_size = floor($file_size/$parts_num);

$modulus=$file_size % $parts_num;

for($i=0;$i<$parts_num;$i++)

{

if($modulus!=0 & $i==$parts_num-1)

$parts[$i] = fread($handle,$parts_size+$modulus) or die("error reading file");

else

$parts[$i] = fread($handle,$parts_size) or die("error reading file");

}

//close file handle

fclose($handle) or die("error closing file handle");

//writing to splitted files

for($i=0;$i<$parts_num;$i++)

{

$handle = fopen(‘splited_’.$i, ‘wb’) or die("error opening file for writing");

fwrite($handle,$parts[$i]) or die("error writing splited file");

}

//close file handle

fclose($handle) or die("error closing file handle");

return ‘OK';

}

To call the function in the script:

splitthisfile($file_name,$parts_num) or die(‘Error spliting file’);

This ends the first part. In the second part you will learn how to create the merging script and actually implement those files in your FTP server. You can download the complete script from the second part.

[gp-comments width="770" linklove="off" ]

antalya escort bayan antalya escort bayan Antalya escort diyarbakir escort