-
Notifications
You must be signed in to change notification settings - Fork 0
Extended Input for Files
An extension to CI_Input to add support for uploaded files.
Extension parses the $_FILES array to reorder the array for better integration with Code Igniter's CI_Upload library.
Use $this->input->file("user_file"); to return an array of uploaded file data to pass to CI_Upload library.
Returned array includes remapped 'key' for ease of use as well as all appropriate $_FILES array key and value pairs.
Note: The class prefix "SM_" and file name should be replaced with the appropriate configuration value ("subclass_prefix") in your config.php file.
The input library from which this extends is loaded automatically by Code Igniter so you don't need to load the library yourself.
To install this extension, simply copy the file into your "application/libraries/" folder and edit the filename and class extension to match the appropriate configuration value, as mentioned above.
Usage:
//gather uploaded file array
$file = $this->input->file("user_file");
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
//*******
//
// filename: SM_Input.php
// author: Zack Brown
// date: 7th September 2010
//
//*******
*/
//declare class
class SM_Input extends CI_Input
{
/*
//*******
//
// member variables
//
//*******
*/
//original, unprocessed $_FILES array
var $_files = array();
//parsed array of file data
var $files = array();
/*
//*******
//
// internal methods
//
//*******
*/
//class constructor
function SM_Input()
{
//call parent constructor
parent::CI_Input();
//parse and reorder $_FILES array
$this->_clean_input_files();
//debug
log_message("debug", "SM_Input Class Initialized");
}
//parse and reorder $_FILES array
function _clean_input_files()
{
//save current $_FILES array
$this->_files = $_FILES;
//reset array of parsed files
$this->files = array();
//check $_FILES array is valid
if(is_array($this->_files) && count($this->_files) > 0)
{
//reset $_FILES array
$_FILES = array();
//loop through array of $_FILES
foreach($this->_files as $outer_key => $outer_value)
{
//count array of files
$count = (is_array($outer_value['name']) ? count($outer_value['name']) : 0);
//check outer value for array of file data
if($count > 0)
{
//loop through array of file data
foreach($outer_value['name'] as $inner_key => $inner_value)
{
//compile file data array key
$key = $outer_key . ($count > 1 ? "_" . $inner_key : "");
//array of file data
$file = array();
//gather file data from array of outer values
$file['error'] = $outer_value['error'][$inner_key];
$file['name'] = $outer_value['name'][$inner_key];
$file['size'] = $outer_value['size'][$inner_key];
$file['tmp_name'] = $outer_value['tmp_name'][$inner_key];
$file['type'] = $outer_value['type'][$inner_key];
//append file key
$file['key'] = $key;
//save file data to $_FILES array
$_FILES[$key] = $file;
//check for single file
if($count == 1)
{
//save file data to array of parsed data
$this->files[$outer_key] = $file;
}
else
{
//save file data to array of parsed data
$this->files[$outer_key][$inner_key] = $file;
}
}
}
else
{
//append file key
$outer_value['key'] = $outer_key;
//save file data to $_FILES array
$_FILES[$outer_key] = $outer_value;
//save file data to array of parsed data
$this->files[$outer_key] = $outer_value;
}
}
}
}
/*
//*******
//
// helper methods
//
//*******
*/
//fetch an item from parsed array of file data
function file($index = "", $xss_clean = false)
{
//return item from parsed array of files
return $this->_fetch_from_array($this->files, $index, $xss_clean);
}
//fetch an item from original array of $_FILES data
function _file($index = "", $xss_clean = false)
{
//return item from $_FILES array
return $this->_fetch_from_array($this->_files, $index, $xss_clean);
}
//return array of parsed file data
function files()
{
//return parsed file array
return $this->files;
}
//return array of original $_FILES
function _files()
{
//return original $_FILES array
return $this->_files;
}
}