Najla Ahmadyar
Najla Ahmadyar

Reputation: 31

Error in codeigniter while file uploading

I have a problem while uploading the file this is the whole controller code: Controller screenshot and this is the file upload code:

    $config['upload_path'] = './_uploads';
		$config['allowed_types'] = 'gif|jpg|png|jpeg';
	
		$this->load->library('Upload');
		$this->upload->initialize($config);
	
		if (!$this->upload->do_upload('image')) {
			echo "failed";
		
		} else {
			echo "sucess";
		}

And when I run this it gives me error:

An Error Was Encountered
Resource 'upload' already exists and is not a CI_Upload instance

Upvotes: 1

Views: 308

Answers (3)

The upload class must have been autoloaded already on your config /autoload.php so reinitializing the upload class will throw an error.

Upvotes: 0

Rohit Mittal
Rohit Mittal

Reputation: 2104

You are loading 'Upload' instead of 'upload' (upload should be in small case). and I also would like to make little more changes

    $config['upload_path'] = './_uploads';
    $config['allowed_types'] = 'gif|jpg|png|jpeg';
    $config['file_name'] = 'filename.png'; //extension should be same as uploaded file

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

    if (!$this->upload->do_upload('image')) {
        echo "failed";

    } else {
        echo "sucess";
    }

Hope it may help you.

Upvotes: 2

Anita Patel
Anita Patel

Reputation: 21

Are you using this code for multiple image upload? If you are using for multiple image then please load upload library $this->load->library('upload') outside loop.

Please provide full code with method name if not multiple images.

Upvotes: 2

Related Questions