模块

File

File helper class.

package
BootPHP
category
辅助类
author
Tinsh
copyright
© 2005-2016 Kilofox Studio

该类在 SYSPATH/classes/file.php 第 13 行声明。

方法

public static ext_by_mime( string $type ) (在 File 中定义)

Lookup a single file extension by MIME type.

参数

  • string $type required - MIME type to lookup

返回值

  • mixed - First file extension matching || false

源代码

public static function ext_by_mime($type)
{
	return current(File::exts_by_mime($type));
}

public static exts_by_mime( string $type ) (在 File 中定义)

Lookup file extensions by MIME type

参数

  • string $type required - File MIME type

返回值

  • array - File extensions matching MIME type

源代码

public static function exts_by_mime($type)
{
	static $types = array();
	// Fill the static array
	if (empty($types))
	{
		foreach (BootPHP::$config->load('mimes') as $ext => $mimes)
		{
			foreach ($mimes as $mime)
			{
				if ($mime == 'application/octet-stream')
				{
					// octet-stream is a generic binary
					continue;
				}
				if (!isset($types[$mime]))
				{
					$types[$mime] = array((string) $ext);
				}
				elseif (!in_array($ext, $types[$mime]))
				{
					$types[$mime][] = (string) $ext;
				}
			}
		}
	}
	return isset($types[$type]) ? $types[$type] : false;
}

public static join( string $filename ) (在 File 中定义)

Join a split file into a whole file. Does the reverse of File::split.

$count = File::join($file);

参数

  • string $filename required - Split filename, without .000 extension

返回值

  • integer - The number of pieces that were joined.

源代码

public static function join($filename)
{
	// Open the file
	$file = fopen($filename, 'wb+');
	// Read files in 8k blocks
	$block_size = 1024 * 8;
	// Total number of peices
	$pieces = 0;
	while (is_file($piece = $filename . '.' . str_pad($pieces + 1, 3, '0', STR_PAD_LEFT)))
	{
		// Read another piece
		$pieces += 1;
		// Open the piece for reading
		$piece = fopen($piece, 'rb');
		while (!feof($piece))
		{
			// Transfer the data in blocks
			fwrite($file, fread($piece, $block_size));
		}
		// Close the peice
		fclose($piece);
	}
	return $pieces;
}

public static mime( string $filename ) (在 File 中定义)

Attempt to get the mime type from a file. This method is horribly unreliable, due to PHP being horribly unreliable when it comes to determining the mime type of a file.

$mime = File::mime($file);

参数

  • string $filename required - File name || path

返回值

  • string - Mime type on success
  • false - On failure

源代码

public static function mime($filename)
{
	// Get the complete path to the file
	$filename = realpath($filename);
	// Get the extension from the filename
	$extension = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
	if (preg_match('/^(?:jpe?g|png|[gt]if|bmp|swf)$/', $extension))
	{
		// Use getimagesize() to find the mime type on images
		$file = getimagesize($filename);
		if (isset($file['mime']))
			return $file['mime'];
	}
	if (class_exists('finfo', false))
	{
		if ($info = new finfo(defined('FILEINFO_MIME_TYPE') ? FILEINFO_MIME_TYPE : FILEINFO_MIME))
		{
			return $info->file($filename);
		}
	}
	if (ini_get('mime_magic.magicfile') && function_exists('mime_content_type'))
	{
		// The mime_content_type function is only useful with a magic file
		return mime_content_type($filename);
	}
	if (!empty($extension))
	{
		return File::mime_by_ext($extension);
	}
	// Unable to find the mime-type
	return false;
}

public static mime_by_ext( string $extension ) (在 File 中定义)

Return the mime type of an extension.

$mime = File::mime_by_ext('png'); // "image/png"

参数

  • string $extension required - Extension: php, pdf, txt, etc

返回值

  • string - Mime type on success
  • false - On failure

源代码

public static function mime_by_ext($extension)
{
	// Load all of the mime types
	$mimes = BootPHP::$config->load('mimes');
	return isset($mimes[$extension]) ? $mimes[$extension][0] : false;
}

public static mimes_by_ext( string $extension ) (在 File 中定义)

Lookup MIME types for a file

参数

  • string $extension required - Extension to lookup

Tags

  • See - BootPHP_File::mime_by_ext()

返回值

  • array - Array of MIMEs associated with the specified extension

源代码

public static function mimes_by_ext($extension)
{
	// Load all of the mime types
	$mimes = BootPHP::$config->load('mimes');
	return isset($mimes[$extension]) ? ( (array) $mimes[$extension]) : array();
}

public static split( string $filename [, string $piece_size = integer 10 ] ) (在 File 中定义)

Split a file into pieces matching a specific size. Used when you need to split large files into smaller pieces for easy transmission.

$count = File::split($file);

参数

  • string $filename required - File to be split
  • string $piece_size = integer 10 - Directory to output to, defaults to the same directory as the file

返回值

  • integer - The number of pieces that were created

源代码

public static function split($filename, $piece_size = 10)
{
	// Open the input file
	$file = fopen($filename, 'rb');
	// Change the piece size to bytes
	$piece_size = floor($piece_size * 1024 * 1024);
	// Write files in 8k blocks
	$block_size = 1024 * 8;
	// Total number of peices
	$peices = 0;
	while (!feof($file))
	{
		// Create another piece
		$peices += 1;
		// Create a new file piece
		$piece = str_pad($peices, 3, '0', STR_PAD_LEFT);
		$piece = fopen($filename . '.' . $piece, 'wb+');
		// Number of bytes read
		$read = 0;
		do
		{
			// Transfer the data in blocks
			fwrite($piece, fread($file, $block_size));
			// Another block has been read
			$read += $block_size;
		}
		while ($read < $piece_size);
		// Close the piece
		fclose($piece);
	}
	// Close the file
	fclose($file);
	return $peices;
}