Welcome Guest, Not a member yet? Register   Sign In
how to use $this->upload->data()
#1

[eluser]snowstar[/eluser]
Hi guys,

Atempting to understand how to get data out of $this->upload->data().

Id like to get the file name of the file thats just been uploaded. For the moment i'm renaming it but i would prefer not to.


My Controller
Code:
function upload()
{
  // Set directories for upload location
  $this->config->load('uploading_script');
  $upload_dir = $this->config->item('upload_dir');
  $uniqueid = $this->session->userdata('uniqueid');
  $rand = rand(1, 128);
  $filename = $uniqueid.$rand;
  
  // Configure Upload class
  $config['upload_path'] = './' . $upload_dir . '/';
  $config['allowed_types'] = $this->config->item('acceptable_files');
  $config['max_size'] = $this->config->item('max_kb');
  $config['max_width']  = $this->config->item('max_width');
  $config['max_height']  = $this->config->item('max_height');
  $config['file_name']  = $filename;
  
  $this->load->library('upload', $config);
  

  $fileinfo = array(
   'uniqueid'  => $uniqueid,
         'filename'     => $filename
  );
  
  $this->entry_model->file_info($fileinfo);

  
  // Output json as response
  if ( ! $this->upload->do_upload())
  {
   $json['status'] = 'error';
   $json['issue'] = $this->upload->display_errors('','');
  
  }
  else
  {
   $json['status'] = 'success';
   foreach($this->upload->data() as $k => $v)
   {
    $json[$k] = $v;
   }
  }

  echo json_encode($json);

}

Id like to insert the file name into my db like:

Code:
$fileinfo = array(
   'uniqueid'  => $uniqueid,
         'filename'     => $filename
  );
  
  $this->entry_model->file_info($fileinfo);

I know it can be done as its been done here, how-ever i didn't originally write the code.

Code:
$json['status'] = 'success';
   foreach($this->upload->data() as $k => $v)
   {
    $json[$k] = $v;
   }

Im not able to wrap my head around the correct format to access the data i.e $data['file_name']

Any help is greatly appreciated
#2

[eluser]ebuoe[/eluser]
to get all the data from the recent upload you use

Code:
$file_data = $this->upload->data();

where $file_data is a variable that gets all the properties of the uploaded file as an array

so to get the file name from the array u do :

Code:
$file_name = $file_data['file_name'];

now to insert into the database using your array you do :

Code:
$fileinfo = array(
   'uniqueid'  => $uniqueid,
         'filename'     => $file_name
  );
  
  $this->entry_model->file_info($fileinfo);

this should solve the problem

and just in case you need any other data from the file you just uploaded e.g. size,extension,image_height etc you just access the $file_data variable the same way
you got the the value for file name

i.e.
Code:
variable = $file_data['data_you_need'];

e.g to get the file extension you can do
$file_ext = $file_data['file_ext']; // result :: .jpg or .png or .gif dependn on ur xtension

$fileSize = $file_data['file_size']; // result :: the size of the file in kilo bytes

//you can check for other parameters in the "file uploading class section" in the documentation


i hope this will be helpful Smile





#3

[eluser]snowstar[/eluser]
Thanks! worked perfectly
#4

[eluser]ebuoe[/eluser]
you are most welcome




Theme © iAndrew 2016 - Forum software by © MyBB