Codeigniter image upload will only upload to one d

2019-05-22 13:22发布

问题:

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:

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.



回答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!



回答3:

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.



回答4:

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.


回答5:

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