3
function upload($path){
  $config['overwrite'] = TRUE;
  $config['allowed_types'] = 'jpg|jpeg|gif|png';
  $config['max_size'] = 2000;

  if($path =='profile'){
   $config['upload_path'] = '../assets/uploads/avatars';
   $config['file_name'] = $this->id;  
  }
  if($path =='company'){
   $config['upload_path'] = '../assets/uploads/company';
   $config['file_name'] = $this->id.'_company';
  }

  $this->load->library('upload', $config);
  $this->upload->do_upload();

  $image_data = $this->upload->data();
  if($path == 'profile'){
   $this->db->update('be_user_profiles',array('avatar' => $image_data['file_name']), array('user_id' => $this->id));
  }
  if($path == 'company'){
   $this->db->update('be_user_profiles',array('company_logo' => $image_data['file_name']), array('user_id' => $this->id));
  }
  $config = array(
   'source_image' => $image_data['full_path'],
   'new_image' => $this->upload_path,
   'maintain_ratio' => true,
   'width' => 500,
   'height' => 500
  );

  $this->load->library('image_lib', $config);
  $this->image_lib->resize();
 }

This is my upload function, and it works fine when $path = 'profile', but when it is company, it won't upload to the "company" folder... Is there any reason it should be so?! I'm at a loss here...This function works when it goes to the avatar folder, but not if it goes to the company folder...

1
  • replace $this->load->library('image_lib', $config); by $this->load->library('image_lib'); $this->image_lib->initialize($config); Commented Mar 3, 2015 at 10:55

5 Answers 5

13

I had a similar problem with multiple folders. The way I fixed was to use to use the initialize function instead of passing the config as an argument to the load library function.

$this->upload->initialize($config);

You could load the library then set your config and call the initialize method.

Hope this helps.

Sign up to request clarification or add additional context in comments.

2 Comments

I don't even know what the problem was...but I used this for the path: realpath(APPPATH . '../assets/uploads/company');...
ifaour made some very good suggestions. I would make sure you try what he says. The CI docs have the path setup this way: './uploads' where the dot denotes from the root of the site. So the assuming your site is at mysite.com the uploads directory would be at mysiste.com/uploads.
2

I think all suggestions are really good, you can also make sure you are posting the right data in your form: $this->upload->do_upload(); is by default expecting the form name to be 'userfile'

also I find sometimes really usefull to have some errors displayed...so instead of just $this->upload->do_upload(); try something like

if (!$this->upload->do_upload($userfile)) {
            $error = array('error' => $this->upload->display_errors());

            $this->load->view('upload_error', $error);
        }

and then add a file in your views called upload_error.php with the following code in it:

<html>
<head>
<title>Upload Form</title>
</head>
<body>

<?php echo $error;?>



</body>
</html>

good luck!

Comments

2

Try to unset config before setting another config like this. The following code unset config and clear image library.

You need to clear your lib.

//Unset config for the next one
  unset($config);
  $this->image_lib->clear();

From CI manual...

$this->image_lib->clear()

The clear function resets all of the values used when processing an image. You will want to call this if you are processing images in a loop.

Comments

0

Hmmm, Here are two suggestions:

  1. Make sure your company folder is writable
  2. What $this->id returns? if it's returning the file name WITH the extension then this is what causing your upload process to fail because then you'll have a file name my_image.png_company which is not an allowed type.
    Please refer to the documentation page for more info.

2 Comments

$this->id is simply the user's id. The upload class automagically adds the extension after the file is named.
are you sure about that? what version of CI you are working on?
0
 $config = array(
    'upload_path'   './assets/upload/profilepic/',
    'allowed_types' => 'JPG|JPEG|PNG',
    'overwrite' => TRUE,
    'file_name'  => date('YmdHis').''.rand(0,9999)
); 

its not works for JPEG file type also i had written in small latter but it didn't works. please Share if you have something JPEG files are not upload Others file are inserted easely.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.