In view
<?php echo form_open_multipart('welcome/do_upload');?>
<input type="file" name="userfile" size="20" />
In controler
function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['overwrite'] = TRUE;
$config['encrypt_name'] = FALSE;
$config['remove_spaces'] = TRUE;
if ( ! is_dir($config['upload_path']) ) die("THE UPLOAD DIRECTORY DOES NOT EXIST");
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('userfile')) {
echo 'error';
} else {
return array('upload_data' => $this->upload->data());
}
}
And I call this function like this
$this->data['data'] = $this->do_upload();
and view this image:
<ul>
<?php foreach ($data['upload_data'] as $item => $value):?>
<li><?php echo $item;?>: <?php echo $value;?></li>
<?php endforeach; ?>
</ul>
I don't know what's the error.
It seems the problem is you send the form request to
welcome/do_upload
, and call theWelcome::do_upload()
method in another one by$this->do_upload()
.Hence when you call the
$this->do_upload();
within your second method, the$_FILES
array would be empty.And that's why
var_dump($data['upload_data']);
returnsNULL
.If you want to upload the file from
welcome/second_method
, send the form request to the welcome/second_method where you call$this->do_upload();
.Then change the form helper function (within the View) as follows1:
File Uploading with CodeIgniter
CodeIgniter has documented the Uploading process very well, by using the File Uploading library.
You could take a look at the sample code in the user guide; And also, in order to get a better understanding of the uploading configs, Check the Config items Explanation section at the end of the manual page.
Also there are couple of articles/samples about the file uploading in CodeIgniter, you might want to consider:
Just as a side-note: Make sure that you've loaded the
url
andform
helper functions before using the CodeIgniter sample code:1. The form must be "multipart" type for file uploading. Hence you should use
form_open_multipart()
helper function which returns:<form method="post" action="controller/method" enctype="multipart/form-data" />
Below code for an uploading a single file at a time. This is correct and perfect to upload a single file. Read all commented instructions and follow the code. Definitely, it is worked.
File uploads are an essential process in many web apps. Almost every website and web app requires an integrated file upload component. File and image upload in CodeIgniter powered app is a simple component that takes care of the upload process with little issues.
Create the Controller
The next step is the creation of a file in the controller folder. Name the file upload_controller.php. In this file, I will load a library for initializing the Upload class through the following code:
I will also set the preferences for the file upload process through the controller function
do_upload()
. this function will contain the following code:For further detail please have a look https://www.cloudways.com/blog/codeigniter-upload-file-image/
Simple Image upload in codeigniter
Find below code for easy image upload
Hope this helps you to upload image
Change the code like this. It works perfectly: