• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Image Uploads

#1
[eluser]Benjamin Bowles[/eluser]
I'm having some trouble get a file upload code working.

I've got a controller that calls a function from a library that I've created, this happens each time I want an image uploaded, you can see this in the snippet of code from the controller that is calling the function:

Code:
if(isset($_POST['addProof'])) {
  $aupload = $this->proofing->uploadImage('./assets/artwork/', 'jpg', 'artwork');
  $iupload = $this->proofing->uploadImage('./assets/invoice/', 'pdf', 'invoice');
  $this->proofs_model->add($aupload, $iupload);
  redirect('/proof');
}

The code for the uploadImage() function is below:

Code:
function uploadImage($path = '', $types = '', $file = '') {
  $CI =& get_instance();
  $config['upload_path'] = $path;
  $config['allowed_types'] = $types;
  $config['encrypt_name'] = TRUE;
  $CI->load->library('upload', $config);
  
  if($CI->upload->do_upload($file)) {
   $d = $CI->upload->data();
   $filename = $d['file_name'];
   return $filename;
  } else {                
   return '';                        
  }
}

The call to the uploadImage() function seems to work the first time (for the artwork upload), but for the second upload (invoice) it seems to fail.

Is there some sort of upload->clear() that I should do each time?

Any help would be truly appreciated!

#2
[eluser]umefarooq[/eluser]
do_upload will not under stand $file it always understand $_FILES parameter so convert the $file parameter to following in you function.

Code:
$up = 'upload';
$_FILES['upload']['name'] = $file['name'];
        $_FILES['upload']['type'] = $file['type'];
        $_FILES['upload']['tmp_name'] = $file['tmp_name'];
        $_FILES['upload']['error'] = $file['error'];
        $_FILES['upload']['size'] = $file['size'];

$this->upload->do_upload($up)

it will work fine.

#3
[eluser]Evil Wizard[/eluser]
I think the $file variable contains the name of the input for the $_FILES

You need to ensure that both pdf and the image types are in the allowed_types, and there used to be a bug that hindered non image uploads if the allowed_types contained image types, but I think that bug has been cleared up now

#4
[eluser]Benjamin Bowles[/eluser]
[quote author="umefarooq" date="1246906752"]do_upload will not under stand $file it always understand $_FILES parameter so convert the $file parameter to following in you function.

Code:
$up = 'upload';
$_FILES['upload']['name'] = $file['name'];
        $_FILES['upload']['type'] = $file['type'];
        $_FILES['upload']['tmp_name'] = $file['tmp_name'];
        $_FILES['upload']['error'] = $file['error'];
        $_FILES['upload']['size'] = $file['size'];

$this->upload->do_upload($up)

it will work fine.[/quote]

The first upload that is used within the controller seems to work, like simply if i swap them around the first one will always work.

umefarooq I tried your method and it ended up that none of the uploads worked! Sorry buddy.

#5
[eluser]Evil Wizard[/eluser]
I'm not sure how the loader really works but If I had coded it I wouldn't instantiate the object every time it was loaded, I'd check to see if it was already loaded and return that instance. If that is the case, then only the first instantiation of the object, and its config would be return on the second run.
try this instead:
Code:
function uploadImage($path = '', $types = '', $file = '') {
  $CI =& get_instance();
  $config['upload_path'] = $path;
  $config['allowed_types'] = $types;
  $config['encrypt_name'] = TRUE;
  $CI->load->library('upload');
  $CI->upload->initialize($config);
  
  if($CI->upload->do_upload($file)) {
   $d = $CI->upload->data();
   $filename = $d['file_name'];
   return $filename;
  } else {                
   return '';                        
  }
}
by sending the config data to the initialize method the config details are effectively reset and ready for the second upload.

#6
[eluser]Benjamin Bowles[/eluser]
[quote author="Evil Wizard" date="1246907677"]I'm not sure how the loader really works but If I had coded it I wouldn't instantiate the object every time it was loaded, I'd check to see if it was already loaded and return that instance. If that is the case, then only the first instantiation of the object, and its config would be return on the second run.
try this instead:
Code:
function uploadImage($path = '', $types = '', $file = '') {
  $CI =& get_instance();
  $config['upload_path'] = $path;
  $config['allowed_types'] = $types;
  $config['encrypt_name'] = TRUE;
  $CI->load->library('upload');
  $CI->upload->initialize($config);
  
  if($CI->upload->do_upload($file)) {
   $d = $CI->upload->data();
   $filename = $d['file_name'];
   return $filename;
  } else {                
   return '';                        
  }
}
by sending the config data to the initialize method the config details are effectively reset and ready for the second upload.[/quote]

Fantastic - worked a treat. Job Done.

Thanks Evil Wizard, not so evil are you!

#7
[eluser]Evil Wizard[/eluser]
Evil is only evil from a certain point of view Wink glad to help Smile


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.