Hello everyone.
I have tried several extensions for handling file uploads, but I am not satisfied entirely.
I need to be able to handle regular files (Word and Excel files, for example) as well as images for saving and post processing.
So, I decided that I needed to separate the upload itself from the actions I need to perform on different kind of files.
I created an upload behavior, which basically checks for errors uploading and validates some basic options, like file size and type and it works fine.
But I got stuck on the next step, which is to use that file for something.
So, I’ve got my controller:
class SubirController extends Controller
{
public function behaviors()
{
return array(
'uploadFile'=>array(
'class'=>'application.components.behaviors.UploadFile',
),
);
}
public function actionDoSomething()
{
/* here I want to do something with the uploaded file */
}
And this is the behavior:
class UploadFile extends CBehavior {
// PHP File Upload error message codes:
// http://php.net/manual/en/features.file-upload.errors.php
protected $error_messages = array(
1 => 'The uploaded file exceeds the upload_max_filesize directive in php.ini',
2 => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form',
3 => 'The uploaded file was only partially uploaded',
4 => 'No file was uploaded',
6 => 'Missing a temporary folder',
7 => 'Failed to write file to disk',
8 => 'A PHP extension stopped the file upload',
'max_file_size' => 'File is too big',
'min_file_size' => 'File is too small',
'accept_file_types' => 'Filetype not allowed',
'max_number_of_files' => 'Maximum number of files exceeded',
'max_width' => 'Image exceeds maximum width',
'min_width' => 'Image requires a minimum width',
'max_height' => 'Image exceeds maximum height',
'min_height' => 'Image requires a minimum height',
'min_height' => 'Image requires a minimum height',
'missingFileName' => 'Unnamed file',
'uploaded_file' => 'Error identifying uploaded file'
);
public $files;
public function __construct()
{
$this->start();
}
public function rearrange_array()
{
$uploaded_files = array();
$var_name = array_keys($_FILES);
$var = $var_name[0];
if (sizeof($_FILES[$var]) > 0) {
foreach($_FILES[$var]['name'] as $n=>$data) {
$uploaded_files[] = array(
'name'=>$_FILES[$var]['name'][$n],
'tmp_name'=>$_FILES[$var]['tmp_name'][$n],
'error'=>$_FILES[$var]['error'][$n],
'type'=>$_FILES[$var]['type'][$n],
'size'=>$_FILES[$var]['size'][$n],
);
}
}
return $uploaded_files;
}
protected function get_error_message($error) {
return array_key_exists($error, $this->error_messages) ?
$this->error_messages[$error] : $error;
}
public function start()
{
$files = array();
if (sizeof($_FILES) > 0) {
$uploaded_files = $this->rearrange_array();
foreach($uploaded_files as $file) {
$uploaded_file['error'] = 1;
$file['error_message'] = $this->get_error_message($uploaded_file['error']);
if ($file['error_message'] == '') {
if ($file['name'] != '') {
if ($file['size'] > 0) {
if (is_uploaded_file($file['tmp_name'])) {
} else {
$file['error_message'] = $this->get_error_message('uploaded_file');
}
} else {
$file['error_message'] = $this->get_error_message('min_file_size');
$file['status'] = false;
}
} else {
$file['error_message'] = $this->get_error_message('missingFileName');
$file['estatus'] = false;
}
} else {
$file['estatus'] = false;
}
$files[] = $file;
}
}
echo CJSON::encode($files);
}
}
When I run the controller, I get the JSON response on my browser, but what I want to do is, once the file is uploaded and checked by the behavior, do something with the file with a function or action, like read an Excel file or manipulate a JPG.
How do I communicate the behavior and the action? Or maybe I am using a wrong approach?
Thanks!