Home arrow PHP arrow Page 4 - How to Split a File During an FTP Upload Using PHP

Using the File Splitting Function - 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.

TABLE OF CONTENTS:
  1. How to Split a File During an FTP Upload Using PHP
  2. Inside the Split Folder Directory
  3. PHP File Splitting Script: splitthisfile.php
  4. Using the File Splitting Function
By: Codex-M
Rating: starstarstarstarstar / 3
November 17, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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.



 
 
>>> 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: