|
<?php defined('SYSPATH') or die('No direct script access.');
|
|
/**
|
|
* File helper class.
|
|
*
|
|
* $Id: file.php 1725 2008-01-17 16:38:59Z PugFish $
|
|
*
|
|
* @package File Helper
|
|
* @author Kohana Team
|
|
* @copyright (c) 2007-2008 Kohana Team
|
|
* @license http://kohanaphp.com/license.html
|
|
*/
|
|
class file {
|
|
|
|
/**
|
|
* Split a file into pieces matching a specific size.
|
|
*
|
|
* @param string file to be split
|
|
* @param string directory to output to, defaults to the same directory as the file
|
|
* @param integer size, in MB, for each chunk to be
|
|
* @return integer The number of pieces that were created.
|
|
*/
|
|
public static function split($filename, $output_dir = FALSE, $piece_size = 10)
|
|
{
|
|
// Find output dir
|
|
$output_dir = ($output_dir == FALSE) ? pathinfo(str_replace('\\', '/', realpath($filename)), PATHINFO_DIRNAME) : str_replace('\\', '/', realpath($output_dir));
|
|
$output_dir = rtrim($output_dir, '/').'/';
|
|
|
|
// Open files for writing
|
|
$input_file = fopen($filename, 'rb');
|
|
|
|
// Change the piece size to bytes
|
|
$piece_size = 1024 * 1024 * (int) $piece_size; // Size in bytes
|
|
|
|
// Set up reading variables
|
|
$read = 0; // Number of bytes read
|
|
$piece = 1; // Current piece
|
|
$chunk = 1024 * 8; // Chunk size to read
|
|
|
|
// Split the file
|
|
while ( ! feof($input_file))
|
|
{
|
|
// Open a new piece
|
|
$piece_name = $filename.'.'.str_pad($piece, 3, '0', STR_PAD_LEFT);
|
|
$piece_open = @fopen($piece_name, 'wb+') or die('Could not write piece '.$piece_name);
|
|
|
|
// Fill the current piece
|
|
while ($read < $piece_size AND $data = fread($input_file, $chunk))
|
|
{
|
|
fwrite($piece_open, $data) or die('Could not write to open piece '.$piece_name);
|
|
$read += $chunk;
|
|
}
|
|
|
|
// Close the current piece
|
|
fclose($piece_open);
|
|
|
|
// Prepare to open a new piece
|
|
$read = 0;
|
|
$piece++;
|
|
|
|
// Make sure that piece is valid
|
|
($piece < 999) or die('Maximum of 999 pieces exceeded, try a larger piece size');
|
|
}
|
|
|
|
// Close input file
|
|
fclose($input_file);
|
|
|
|
// Returns the number of pieces that were created
|
|
return ($piece - 1);
|
|
}
|
|
|
|
/**
|
|
* Join a split file into a whole file.
|
|
*
|
|
* @param string split filename, without .000 extension
|
|
* @param string output filename, if different then an the filename
|
|
* @return integer The number of pieces that were joined.
|
|
*/
|
|
public static function join($filename, $output = FALSE)
|
|
{
|
|
if ($output == FALSE)
|
|
$output = $filename;
|
|
|
|
// Set up reading variables
|
|
$piece = 1; // Current piece
|
|
$chunk = 1024 * 8; // Chunk size to read
|
|
|
|
// Open output file
|
|
$output_file = @fopen($output, 'wb+') or die('Could not open output file '.$output);
|
|
|
|
// Read each piece
|
|
while ($piece_open = @fopen(($piece_name = $filename.'.'.str_pad($piece, 3, '0', STR_PAD_LEFT)), 'rb'))
|
|
{
|
|
// Write the piece into the output file
|
|
while ( ! feof($piece_open))
|
|
{
|
|
fwrite($output_file, fread($piece_open, $chunk));
|
|
}
|
|
|
|
// Close the current piece
|
|
fclose($piece_open);
|
|
|
|
// Prepare for a new piece
|
|
$piece++;
|
|
|
|
// Make sure piece is valid
|
|
($piece < 999) or die('Maximum of 999 pieces exceeded');
|
|
}
|
|
|
|
// Close the output file
|
|
fclose($output_file);
|
|
|
|
// Return the number of pieces joined
|
|
return ($piece - 1);
|
|
}
|
|
|
|
public static function extension($filename)
|
|
{
|
|
$p = explode('.',$filename);
|
|
return strtolower($p[count($p)-1]);
|
|
}
|
|
|
|
public static function shortened_size_to_bytes($size)
|
|
{
|
|
$size = strtolower($size);
|
|
$last = substr($size, -1);
|
|
if ($last == 'b')
|
|
{
|
|
$size = substr($size, 0, -1);
|
|
}
|
|
|
|
$last = substr($size, -1);
|
|
switch ($last)
|
|
{
|
|
case 't':
|
|
$size *= 1024;
|
|
case 'g':
|
|
$size *= 1024;
|
|
case 'm':
|
|
$size *= 1024;
|
|
case 'k':
|
|
$size *= 1024;
|
|
}
|
|
|
|
return $size;
|
|
}
|
|
|
|
} // End file
|