Help Upload Library Please! - El Forum - 02-25-2010
[eluser]Unknown[/eluser]
Hi all, I need help, i'm a newbie in CI.
I know that is inappropriate add a new method directly on a CI lib (in this case Upload).
Whatever, I have a problem.
The thing is i created a method to upload multiple files and works almost fine.
This method i added it to Upload CI class and when i called it from my 'Upload' controller (note: same name than CI Lib) it seems the Upload CI lib load twice!
The METHOD in my Upload CONTROLLER code:
Code: function multiple_files_form()
{
if($this->phpauthent->is_logged_in())
{
$data['header'] = 'Ajax Upload';
//Upload files with single element
$data['extraHeadContent'] = "[removed] arrayExt = new Array('.pdf', '.jpeg', '.jpg', '.png', '.gif'); [removed]";
$data['extraHeadContent'] .= "[removed][removed]";
//Load Script Module
$data['extraHeadContent'] .= "[removed][removed]";
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'pdf|jpeg|jpg|png|gif';
$config['max_size'] = '100';
$this->load->library('upload', $config);
if(isset($_FILES['files']))
{
if (! $this->upload->do_multiple_upload($_FILES['files']))//If there is some error...
{
$data['errors'] = $this->upload->display_errors();
$this->load->view('upload_files', $data);
}
else
{
if($this->upload->do_multiple_upload($_FILES['files']))//If is a succesfull upload...
{
echo implode(" / ", $this->upload->get_unique_file_names());
$success_data['sucess_message'] = 'Los archivos se han subido satisfactoriamente.';
$this->load->view('success', $success_data);
}
}
}
else//If is the first time opening the form...
{
$data['errors'] = '';
$this->load->view('upload_files', $data);
}
}
else
{
redirect('/auth');
}
}
The method added into CI Upload Lib in the next post....
Help Upload Library Please! - El Forum - 02-25-2010
[eluser]Unknown[/eluser]
// --------------------------------------------------------------------
Code: /**
* My extension for multiply files
*/
function do_multiple_upload($file_array)//multidimiensional associative array (eq = $_FILES)
{
//Setting error types
$error_array = array(1 => 'Archivo Vacio.', 2=> 'Extension no permitida.', 3=> 'Especifique rutas validas.', 4=> 'Tamano incorrecto.');
$catched_errors = array();
//Validating upload files
$files_count = count($file_array['tmp_name']) - 1;
for($i=0;$i<$files_count;$i++)
{
// Set the uploaded data as class variables
$this->file_temp = $file_array['tmp_name'][$i];
$this->file_name = $this->_prep_filename($file_array['name'][$i]);
$this->file_size = $file_array['size'][$i];
$this->file_type = preg_replace("/^(.+?);.*$/", "\\1", $file_array['type'][$i]);
$this->file_type = strtolower($this->file_type);
$this->file_ext = $this->get_extension($file_array['name'][$i]);
// Convert the file size to kilobytes
if ($this->file_size > 0)
{
$this->file_size = round($this->file_size/1024, 2);
// Is the file type allowed to be uploaded?
if ( ! $this->is_allowed_filetype())
{
if(! in_array(2, $catched_errors))
{
$catched_errors[] = 2;
}
}
// Is the upload path valid?
if ( ! $this->validate_upload_path())
{
if(! in_array(3, $catched_errors))
{
$catched_errors[] = 3;
}
}
// Is the file size within the allowed maximum?
if ( ! $this->is_allowed_filesize())
{
if(! in_array(4, $catched_errors))
{
$catched_errors[] = 4;
}
}
}
else
{
if(! in_array(1, $catched_errors))
{
$catched_errors[] = 1;
}
}
}
if(!empty($catched_errors[0]))//Is there one or more errors?
{
$error_string = 'Error(es): ';
for($i=0;$i<count($catched_errors);$i++)
{
$error_string .= $error_array[$catched_errors[$i]]." "; //We create the error string
}
$this->set_error($error_string);
return FALSE;//Upload fails !
}
else
{
for($i=0;$i<$files_count;$i++)//If there is no errors, we upload the files
{
// Set the uploaded data as class variables
$this->file_temp = $file_array['tmp_name'][$i];
$this->file_name = $file_array['name'][$i];
// Remove white spaces in the name
if ($this->remove_spaces == TRUE)
{
$this->file_name = preg_replace("/\s+/", "_", $this->file_name);
}
$propietario = "_IDUSER";
$this->unique_file_names[] = uniqid($propietario) .'_~;'.$this->file_name;
move_uploaded_file($this->file_temp, $this->upload_path.$this->file_name);
}
return TRUE;//Upload succesfull !
}
}
/**
* Returns the file names we created in do_multiple_upload() method
*/
function get_unique_file_names()
{
return $this->unique_file_names;
}
Thanks for your help. Greetings from Chile.
|