Welcome Guest, Not a member yet? Register   Sign In
Upload library - Strange behavior
#1

Hi,
I have an issue with Upload library.

I have form with two file upload inputs. One for artist has upload path set up to './public/img/logos/' and one for band logo to './public/img/artists/'. If I go to upload only one of them, everything works ok, but if I want to upload both at once it save both the logo and the artist under './public/img/logos/'.
I can't find out what I do wrong.
Thanks for your help.

Here's my code:
function add_new()
{
$this->load->library('form_validation');

$path['logos'] = './public/img/logos/';
$path['photos'] = './public/img/artists/';

$path['db_logos'] = base_url() . 'public/img/logos/';
$path['db_photos'] = base_url() . 'public/img/artists/';

$logo = $_FILES['logo'];
$photo = $_FILES['photo'];

$data['errors'] = '';

if ($logo['name'] != null){
if (file_exists($path['logos'] . $logo['name'])) {
$error['logo'] = "Logo";
$data['errors'] .= "<p>" . $error['logo'] . " is already uploaded</p>";
} else {
$artist['logo'] = $path['db_logos'] . $logo['name'];
$this->upload_image('logo', $path['logos']);
}
}

if ($photo['name'] != null){
if (file_exists($path['photos'] . $photo['name'])) {
$error['photo'] = "Photo";
$data['errors'] .= "<p>" . $error['photo'] . " is already uploaded</p>";
} else {
$artist['photo'] = $path['db_photos'] . $photo['name'];
$this->upload_image('photo', $path['photos']);
}
}

if ($this->form_validation->run() == false || isset($error)) {
$data['errors'] .= validation_errors();
$this->session->set_userdata($empty_form);
} else {
$this->_insert($artist);
}
}

function upload_image($path, $destination)
{
$config['upload_path'] = $destination;
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000';
$config['max_width'] = '2024';
$config['max_height'] = '2768';

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

if ( ! $this->upload->do_upload($path))
{
return $data['errors'] = array('error' => $this->upload->display_errors("<p style='color: red;'>", "</p>"));
}
else
{
$data = $this->upload->data();

}
}
Reply
#2

I know where the problem is now. It is because if I call the function for second time, library is already loaded and that is why my destination path is not actualized. What I need to do is to close loaded library at the end of the upload_image function but I don't know if it is possible or how to do that?
Reply
#3

Use the initialize() method: http://www.codeigniter.com/user_guide/li...initialize
Reply
#4

(07-14-2015, 11:49 AM)mwhitney Wrote: Use the initialize() method: http://www.codeigniter.com/user_guide/li...initialize

Thank you. It working now.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB