php - Getting file name after uploading the file in CI
527
I want to store file name in a database and file in a folder when I click on the button. File is successfully uploaded but I didn't get the file name. Here is my view :
<?php echo form_open_multipart('home/do_upload');?>
<input type="file" name="userfile" size="20" />
<br /><br />
<input type="submit" value="upload" />
</form>
Here is my controller :
public function do_upload()
{
$this->load->helper(array('form', 'url'));
$image_path = realpath(APPPATH . '../assets/pics');
$config['upload_path'] = $image_path; //base_url('assets/pics'); //APPPATH . 'uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
$upload_data = $this->upload->data();
$file_name = $upload_data['file_name'];
if ( ! $this->upload->do_upload())
{
echo 'error';
}
else
{
echo $file_name .' uploaded...';
}
}
Answer
Solution:
In your success part of upload add variable
$file_data = $this-upload->data();
Answer
Solution:
Pass input field name to the
do_upload()
method: