CodeIgniter Forums
Image Uploads - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Image Uploads (/thread-20328.html)



Image Uploads - El Forum - 07-06-2009

[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!


Image Uploads - El Forum - 07-06-2009

[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.


Image Uploads - El Forum - 07-06-2009

[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


Image Uploads - El Forum - 07-06-2009

[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.


Image Uploads - El Forum - 07-06-2009

[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.


Image Uploads - El Forum - 07-06-2009

[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!


Image Uploads - El Forum - 07-06-2009

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